#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
typedef pair<string,int>P;
int bfs(string s){
  queue<P> que;
  map<string,int> used;
  que.push(P(s,0));
  while(!que.empty()){
    P p = que.front();
    que.pop();
    if(p.first == "01234567")return p.second;
    if(used[p.first])continue;
    //    cout << p.first << ' ' << p.second <<endl;
    string puzzle = p.first,next;
    used[puzzle] = 1;
    for(int i = 0 ; i < 8 ; i++ ){
      char a,b;
      next = puzzle;
      //横方向
      if((puzzle[i+1] == '0' || puzzle[i] == '0') && i+1 < 8  && i != 3){
	next[i+1] = puzzle[i]; 
	next[i] = puzzle[i+1]; 
	que.push(P(next,p.second + 1));
      }
      //縦方向
      next = puzzle;
      if((puzzle[i+4] == '0' || puzzle[i] == '0') && i+4 < 8 ){
	next[i+4] = puzzle[i]; 
	next[i] = puzzle[i+4]; 
	que.push(P(next,p.second + 1));
      }
    }
  }
  return 0;
}

int main(){
  string puzzle;
  while(cin >> puzzle){
    int res = bfs(puzzle);
    cout << res << endl;
  }
  return 0;
}