#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
typedef pair<string,int>P;
int dx[] = {-1,1,-4,4};
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;
    used[p.first]=1; 
    string puzzle = p.first,next;
    int pos;
    for(int i = 0 ;  i < 8 ; i++ )if(puzzle[i] == '0')pos = i;

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

int main(){
  string puzzle;
  char c;
  while(cin >> c){
    puzzle = "";
    puzzle += c;
    for(int i = 1; i < 8 ; i++){
      cin >> c;
      puzzle += c;
    }
    int res = bfs(puzzle);
    cout << res << endl;
  }
  return 0;
}