#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<string>
using namespace std;
const int dx[] = { 1, -1, 4, -4};
#define fr first
#define sc second
typedef pair < string , int > sPi;
string s;
int now;
map< string , int > used;
int bfs(){
  queue< sPi > que;
  que.push(sPi("01234567",0));
  used["01234567"] = 1;
  while(!que.empty()){
    sPi p = que.front();
    que.pop();
    for(int i = 0 ; i < 4 ; i++ ){
      string next = p.fr;
      if(0 <= p.sc+dx[i] && p.sc+dx[i] < 8 && !(p.sc==3&&!i) && !(p.sc==4&&i==1)){
        swap(next[p.sc],next[p.sc + dx[i]]);
        if(!used[next]){
          used[next] = used[p.fr] + 1;
          que.push(sPi(next,p.sc+dx[i]));
        }
      }
    }
  }
  return -1;
}
int main(){
  s.resize(8);
  bfs();
  while(cin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] >> s[5] >> s[6] >> s[7]){
    cout << used[s] - 1 << endl;
  }
}