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

using namespace std;

map<string, int> stoi;
map<int, string> itos;

int solve(string str){
  bool dp[100][40320];
  fill(dp[0], dp[30], false);
  dp[0][stoi[str]] = true;
  for(int i=0;i<99;i++){
    for(int j=0;j<40320;j++){
      if(!dp[i][j]) continue;
      if(j==0) return i;
      string str = itos[j];
      for(int k=0;k<8;k++){
	string tmp;
	if(str[k] != '0') continue;
	if(k!=0 && k!=4){
	  tmp = str;
	  swap(tmp[k], tmp[k-1]);
	  dp[i+1][stoi[tmp]] = true;
	}
	if(k!=7 && k!=3){
	  tmp = str;
	  swap(tmp[k], tmp[k+1]);
	  dp[i+1][stoi[tmp]] = true;
	}
	tmp = str;
	if(k<4){
	  swap(tmp[k], tmp[k+4]);
	  dp[i+1][stoi[tmp]] = true;
	}else{
	  swap(tmp[k], tmp[k-4]);
	  dp[i+1][stoi[tmp]] = true;
	}
      }
    }
  }
  return -1;
}

/*class State{
public:
  string str;
  int cnt;
};



int bfs(string str){
  State u, v;
  u.str = str;
  u.cnt = 0;
  queue<State> Q;
  Q.push(u);
  set<string> vis;
  vis.insert(u.str);
  while(!Q.empty()){
    u = Q.front(); Q.pop();
    if(u.str == "01234567") return u.cnt;
    v.cnt = u.cnt + 1;
    for(int i=0;i<8;i++){
      if(u.str[i] != '0') continue;
      if(i!=0 && i!=4){
	v.str = u.str;
	swap(v.str[i], v.str[i-1]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }
      if(i!=7 && i!=3){
	v.str = u.str;
	swap(v.str[i], v.str[i+1]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }
      v.str = u.str;
      if(i<4){
	swap(v.str[i], v.str[i+4]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }else{
	swap(v.str[i], v.str[i-4]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }
    }
  }
  return 0;
  }*/

main(){
  string str = "01234567";
  int cnt = 0;
  do{
    itos[cnt] = str;
    stoi[str] = cnt++;
  }while(next_permutation(str.begin(), str.end()));
  while(cin >> str){
    char c;
    for(int i=0;i<7;i++){
      cin >> c;
      str += c;
    }
    //cout << bfs(str) << endl;
    cout << solve(str) << endl;
  }
  return 0;
}