#include<iostream>
#include<queue>
#include<map>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
int main(){
  map<string, int> ans;
  queue<string> log;
  string f = "01234567";
  int dx[4] = {1,-1,4,-4};
  log.push(f);
  ans[f] = 0;
  while(!log.empty()){
    string s = log.front();
    int x = s.find("0");
    for(int i=0; i<4; i++){
      int nx=x+dx[i];
      if(nx>=0 && nx<=7 && !(x == 3 && nx == 4) && !(x == 4 && nx == 3)){
	string t = s;
	swap(t[x],t[nx]);
	if(ans.find(t) == ans.end()){
	  ans[t] = ans[s]+1;
	  log.push(t);
	}
      }
    }
    log.pop();
  }
  string n;
  while(getline(cin,n)){
    stringstream ss(n);
    string m;
    while(ss >>n)m+=n;
    cout <<ans[m]<<endl;
  }
  return 0;
}