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

int main(){
  while(true){
    map<string,bool> Puzzle;
    string p;
    rep(i,0,8){
      char c;
      cin >>c;
      p+=c;
    }
    pair<string,int> pa;
    pa.first = p;
    pa.second = 0;
    queue<pair<string,int> > q;
    q.push(pa);
    while(!q.empty()){
      pair<string,int> num=q.front();
      q.pop();
      if(Puzzle[num.first])continue;
      if(num.first == "01234567"){
        cout <<num.second<<endl;
        break;
      }
      Puzzle[num.first]=true;
      int zeropos;
      rep(j,0,8){
        if(num.first[j]=='0'){
          zeropos=j;
          break;
        }
      }
      num.second++;
      if(zeropos!=7 || zeropos!=3){
        swap(num.first[zeropos],num.first[zeropos+1]);
        q.push(num);
        swap(num.first[zeropos],num.first[zeropos+1]);
      }
      if(zeropos!=0 || zeropos!=4){
        swap(num.first[zeropos],num.first[zeropos-1]);
        q.push(num);
        swap(num.first[zeropos],num.first[zeropos-1]);
      }
      if(zeropos>=4){
        swap(num.first[zeropos],num.first[zeropos-4]);
        q.push(num);
        swap(num.first[zeropos],num.first[zeropos-4]);
      }
      if(zeropos<=3){
        swap(num.first[zeropos],num.first[zeropos+4]);
        q.push(num);
        swap(num.first[zeropos],num.first[zeropos+4]);
      }
    }
  }
  return 0;
}

