#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;

  for(int i=0;i<8;i++){
    cin >> a;
    c += a;
  }
 
  que.push(c);
  mp[c] = 0;
  int cnt = 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;
  return (0);
}