#include<bits/stdc++.h>
using namespace std;

typedef struct{
  string str;
  int idx;
}Node;
map<string,int>know;
int v[4]={-1,1,-4,4};

void solve();

int main(){
  char num;
  solve();
  while(1){
    //入力
    string card;
    for(int i=0;i<8;i++){
      if(!(cin>>num))return 0;
      card+=num;
    }
    
    cout<<know[card]<<endl;
  }
}
  void solve(){
    map<string,int>kaku;
    queue<Node>que;
    que.push((Node){"01234567",0});
    kaku["01234567"]=2;
    know["01234567"]=0;
    //幅
    while(!que.empty()){
      Node now=que.front();que.pop();
      string temp;
      
      for(int i=0;i<4;i++){
	temp=now.str;
	if(now.idx+v[i]>=0 || now.idx+v[i]<=7 && (now.idx+v[i]!=4 && now.idx==3) && (now.idx==4 && now.idx+v[i]!=3)){
	  swap(temp[now.idx],temp[now.idx+v[i]]);

	  if(kaku[temp]==2){
	    continue;
	  }
	  kaku[temp]=2;

	  que.push((Node){temp,now.idx+v[i]});
	  know[temp]=know[now.str]+1;
	}
      }
    }
}