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

using namespace std;

string correct = "01234567";

int main(){

  string str;
  while(getline(cin,str)){
    string in;
    for(int i = 0; i < str.length(); i++){
      if(str[i] >= '0' && str[i] <= '9') in += str[i];
    }
    map <string,int> M;
    queue <string> Q;
    M[in] = 0;
    Q.push(in);
    while(!Q.empty()){
      string now = Q.front();
      int zero;
      Q.pop();
      if(now == correct){
	cout << M[now] << endl;
	break;
      }
      for(int i = 0; i < 8; i++){
	if(now[i] == '0') zero = i;
      }
      string next = now;
      char tmp;
      if(zero%4 == 0){
	tmp = next[zero];
	next[zero] = next[zero+1];
	next[zero+1] = tmp;
	if(M[next] == 0){
	  Q.push(next);
	  M[next] = M[now] + 1;
	}
      }else if(zero%4 == 3){
	tmp = next[zero];
	next[zero] = next[zero-1];
	next[zero-1] = tmp;
	if(M[next] == 0){
	  Q.push(next);
	  M[next] = M[now] + 1;
	}
      }else{
	tmp = next[zero];
	next[zero] = next[zero+1];
	next[zero+1] = tmp;
	if(M[next] == 0){
	  Q.push(next);
	  M[next] = M[now] + 1;
	}
	next = now;
	tmp = next[zero];
	next[zero] = next[zero-1];
	next[zero-1] = tmp;
	if(M[next] == 0){
	  Q.push(next);
	  M[next] = M[now] + 1;
	}
      }
      next = now;
      if(zero < 4){
	tmp = next[zero];
	next[zero] = next[zero+4];
	next[zero+4] = tmp;
	if(M[next] == 0){
	  Q.push(next);
	  M[next] = M[now] + 1;
	}
      }else{
	tmp = next[zero];
	next[zero] = next[zero-4];
	next[zero-4] = tmp;
	if(M[next] == 0){
	  Q.push(next);
	  M[next] = M[now] + 1;
	}
      }
    }
  }
  return 0;
}