#include<iostream>
#include<queue>
#include<map>
#include<algorithm>

using namespace std;

const string ans = "01234567";
const int move[] = {1,-1,4,-4};
const int disable[] = {-1,-1,3,4};

int getzero(string in){
  for(int i = 0; i < in.length(); i++)
    if(in[i] == '0') return i;
}

int solve(string s){
  map<string,int> M;
  queue<string> Q;
  Q.push(s);
  M[s] = 1;
  while(!Q.empty()){
    string now = Q.front(); Q.pop();
    if(now == ans) return M[now]-1;
    int zpos = getzero(now);
    for(int i = 0; i < 4; i++){
      string next = now;
      int npos = zpos+move[i];
      if(npos >= 0 && npos < 8 && zpos != disable[i]){
	swap(next[zpos],next[npos]);
	if(M[next] == 0){
	  Q.push(next);
	  M[next] = M[now]+1;
	  if(now == ans) return M[now]-1;
	  // cout << next  <<" " <<  M[next]<< endl;
	}
      }
    }
  }
  return -1;
}

int main(){
  string in;
  while(getline(cin,in)){
    string s;
    for(int i = 0; i < in.length(); i++){
	if(in[i] >= '0' && in[i] <= '9') s+=in[i];
    }
    cout << solve(s) << endl;
  }
}