#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back

typedef struct{
  int pat;
  int te,pos0;
}node;

void swap(int&ta,int p,int t){
  stringstream in,out;
  in<<ta;
  string str=in.str();
  if(str.size()<8)str='0'+str;
  swap(str[p],str[t]);
  out<<str;
  out>>ta;
}

main(){
  queue<node> Q;
  Q.push((node){1234567,0,0});
  map<int,int> memo;
  memo[1234567]=0;
  while(!Q.empty()){
    int tvi=Q.front().pat;
    int te=Q.front().te,pos0=Q.front().pos0;
    Q.pop();
    int n=tvi;

    if(pos0%4){
      swap(n,pos0,pos0-1);
      if(memo.count(n)==0){
        memo[n]=te+1;
        Q.push((node){n,te+1,pos0-1});
      }
    }

    n=tvi;
    if(pos0%4!=3){
      swap(n,pos0,pos0+1);
      if(memo.count(n)==0){
        memo[n]=te+1;
        Q.push((node){n,te+1,pos0+1});
      }
    }

    n=tvi;
    swap(n,(pos0+4)%8,pos0);
    if(memo.count(n)==0){
      memo[n]=te+1;
      Q.push((node){n,te+1,(pos0+4)%8});
    }
  }

  int goal=1234567;
  
  int s;
  while(cin>>s){
    int pos0=7;
    int start=s;
    rep(i,7){
      if(!s)pos0=i;
      cin>>s;
      start=start*10+s;
    }
    cout<<memo[start]<<endl;
  }
}