#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
typedef pair<string,int>P;
int dx[] = {-1,1,-4,4};
map<string,int> mp;

void bfs(){
  queue<string> que;

  que.push("01234567");

  while(!que.empty()){
    string p = que.front();
    que.pop();
    string next;
    int pos;
    for(int i = 0 ;  i < 8 ; i++ )if(p[i] == '0')pos = i;

    for(int i = 0 ; i < 4 ; i++ ){
      next = p;
      if(0 <= pos + dx[i] && pos + dx[i] < 8 && !(pos == 3 && i == 1) 
	 && !(pos == 4 && i == 0 )){
	swap(next[pos],next[pos + dx[i]]);
	if(mp[next] == 0 ){
	  que.push(next);
	  mp[next] = mp[p] + 1;
	}
      }
    }
  }
}

int main(){
  string puzzle;
  char c;

  mp["01234567"] = 1;
  bfs();

  while(cin >> c){
    puzzle = "";
    puzzle += c;
    for(int i = 1; i < 8 ; i++){
      cin >> c;
      puzzle += c;
    }
    cout << mp[puzzle]-1 << endl;
  }
  return 0;
}