#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair< vector<int>, int> P;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
vector<int> s(8); 
vector<int> e(8);
bool a[80000000];
int vtoi(vector<int> vec){
  int ret = 0;
  REP(i,vec.size()){
    ret += vec[i]*pow(10,i);
  }
  return ret;
}
int main(void){
  REP(i,8)e[i] = i;
  while(cin>>s[0]){
    memset(a,0,sizeof(a));
    REP(i,7)cin>>s[i+1]; 
    queue<P> que;
    que.push(P(s,0));
    while(!que.empty()){
      P nq = que.front();
      que.pop();
      vector<int> n = nq.first;
      int time = nq.second;
      if(n==e){
        cout<<time<<endl;
        break;
      }
      a[ vtoi(n) ] = true;
      int zeroi = -1;
      REP(i,8)if(n[i]==0) zeroi = i;
      if(zeroi<4){
        vector<int> tmp = n;
        swap(tmp[zeroi],tmp[zeroi+4]);
        if(!a[vtoi(tmp)])
          que.push(P(tmp,time+1));
      }
      if(zeroi>3){
        vector<int> tmp = n;
        swap(tmp[zeroi],tmp[zeroi-4]);
        if(!a[vtoi(tmp)])
          que.push(P(tmp,time+1));
      }
      if(zeroi!=0&&zeroi!=4){
        vector<int> tmp = n;
        swap(tmp[zeroi],tmp[zeroi-1]);
        if(!a[vtoi(tmp)])
          que.push(P(tmp,time+1));
      }
      if(zeroi!=3&&zeroi!=7){
        vector<int> tmp = n;
        swap(tmp[zeroi],tmp[zeroi+1]);
        if(!a[vtoi(tmp)])
          que.push(P(tmp,time+1));
      }
    }
  }
  return 0;
}