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

using namespace std;

const string goal = "01234567";

int dx[] = {1,-1,4,-4};

int getpos(string s){

  for(int i = 0; i < s.length(); i++)
    if(s[i] == '0') return i;

  return 0;
}

vector<int> getable(int n){
  vector<int> V;

  for(int i = 0; i < 4; i++){
    int nex = n+dx[i];
    if(0 <= nex && nex <= 7){
      if(!(n == 3 && nex == 4 || n == 4 && nex == 3)) V.push_back(nex);
    }
  }
  /*
  for(int i = 0; i < V.size(); i++)
    cout << V[i] << " " ;
    cout << endl;
  */
  return V;
}

int main(){

  int data[8];
  
  while(1){


    map<string,int> M;
    queue<string> Q;

    for(int i = 0; i < 8; i++)
      if(!(cin >> data[i])) return 0;


    string s = "";

    for(int i = 0; i < 8; i++)
      s += data[i]+'0';

    if(s == goal) cout << "0" << endl;
    else{
      M[s] = 1;
      Q.push(s);

      bool flg = false;

      while(!Q.empty()){
	string ss = Q.front();
	Q.pop();

	int pos = getpos(ss);

	vector<int> able = getable(pos);

	for(int i = 0; i < able.size(); i++){
	  string next = ss;
	  swap(next[pos],next[able[i]]);

	  if(next == goal){
	    cout << M[ss] << endl;
	    flg = true;
	    break;
	  }else if(M[next] == 0){
	    M[next] = M[ss]+1;
	    Q.push(next);
	  }
	}
	if(flg) break;
      }
    }
    
  }

  return 0;
}