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

using namespace std;

const string goal = "01234567";
typedef pair<string,int> P;
map<string,int> M;

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);
    }
  }
  return V;
}

void make(){
  queue<string> Q;

  Q.push(goal);
  M[goal] = 1;

  while(!Q.empty()){
    string s = Q.front();
    Q.pop();
    int pos = getpos(s);
    vector<int> able = getable(pos);

    for(int i = 0; i < able.size(); i++){
      string next = s;
      swap(next[pos],next[able[i]]);
      if(M[next] == 0){
	M[next] = M[s]+1;
	Q.push(next);
      }
    }
  }
}
int main(){
  make();

  int data[8];

  while(1){
    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';
    
    cout << M[s]-1<< endl;
  }
  return 0;
}