#include<iostream>
#include<string>
#include<queue>
#include<map>
using namespace std;
int main(){
  
  string c,a;
  int d[4] = {1,-1,4,-4};
  queue<string> que;
  map<string,int> mp;

  while(cin >> a){
    mp.clear();
    c = "";
    c += a;
    for(int i=1;i<8;i++){
      cin >> a;
      c += a;
    }
    
    que.push(c);
    mp[c] = 0;
    while(!que.empty() && mp.find("01234567") == mp.end() ){
      
      int i = que.front().find('0');
      for(int j=0;j<4;j++){
	
	if(i+d[j]<0||i+d[j]>=8) continue;
	if(i == 3 && d[j] == 1) continue;
	if(i == 4 && d[j] == -1) continue;
	
	string str = que.front();
	swap(str[i],str[i+d[j]]);
	if(mp.find(str) == mp.end()){
	  que.push(str);
	  mp[str] = mp[que.front()]+1;
	}
      }
      que.pop();
    }
    
    cout << mp["01234567"] << endl;
    while(!que.empty()) que.pop();
  }
  return (0);
}