#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i = a;i<n;i++)

int tenPow(int num){
  int ans =1;
  rep(i,0,num){
    ans*=10;
  }
  return ans;
}
int main(){
  while(true){
    map<int,bool> Puzzle;
    int p=0;
    int cnum;
    string c;
    rep(i,0,8){
      cin >>c;
      if(cin.eof())break;
      stringstream ss;
      ss << c;
      ss >> cnum;
      rep(j,0,7-i){
        cnum*=10;
      }
      p+=cnum;
    }
    pair<int,int> pa;
    pa.first = p;
    pa.second = 0;
    queue<pair<int,int> > q;
    q.push(pa);
    while(!q.empty()){
      pair<int,int> num=q.front();
      q.pop();
      if(Puzzle[num.first]==true)continue;
      if(num.first == 1234567){
        cout <<num.second<<endl;
        break;
      }
      Puzzle[num.first]=true;
      int zeropos;
      int tmp=num.first;
      for(int i = 0;true;i++){
        if(tmp%10==0){
          zeropos=i;
          break;
        }
        tmp/=10;
      }
      num.second++;
      tmp = num.first;
      if(!(zeropos==7 || zeropos==3)){
        int swapnum=(tmp/tenPow(zeropos+1))%10;
        swapnum =tmp - swapnum*tenPow(zeropos+1)+swapnum*tenPow(zeropos);
        pair<int,int> pushnum;
        pushnum.first=swapnum;
        pushnum.second=num.second;
        q.push(pushnum);
      }
      if(!(zeropos==4 || zeropos==0)){
        int swapnum=(tmp/tenPow(zeropos-1))%10;
        swapnum =tmp - swapnum*tenPow(zeropos-1)+swapnum*tenPow(zeropos);
        pair<int,int> pushnum;
        pushnum.first=swapnum;
        pushnum.second=num.second;
        q.push(pushnum);
      }
      if(zeropos>=4){
        int swapnum=(tmp/tenPow(zeropos-4))%10;
        swapnum =tmp - swapnum*tenPow(zeropos-4)+swapnum*tenPow(zeropos);
        pair<int,int> pushnum;
        pushnum.first=swapnum;
        pushnum.second=num.second;
        q.push(pushnum);
      }
      if(zeropos<=3){
        int swapnum=(tmp/tenPow(zeropos+4))%10;
        swapnum =tmp - swapnum*tenPow(zeropos+4)+swapnum*tenPow(zeropos);
        pair<int,int> pushnum;
        pushnum.first=swapnum;
        pushnum.second=num.second;
        q.push(pushnum);
      }
    }
  }
  return 0;
}

