#include<iostream>
using namespace std;
int p[4][2];
bool fn();
bool ans(int n, int l, int x, int y);
int main(){
  int n,x,y;
  while(cin >>n){
    for(int i=0; i<8; i++){
      if(i!=0) cin >>n;
      p[i%4][i/4] = n;
      if(n == 0){
	x = i%4;
	y = i/4;
      }
    }
    for(int i=0;;i++){
      if(ans(i,0,x,y)){
	cout <<i<<endl;
	break;
      }
    }
  }
  return 0;
}
bool fn(){
  for(int i=0; i<8; i++) if(p[i%4][i/4]!=i) return false;
  return true;
}
bool ans(int n, int l, int x, int y){
  int f = false;
  if(n == 0) return fn();
  if(x>0 && l != 2){
    p[x][y] = p[x-1][y];p[x-1][y] = 0;
    f|=ans(n-1,1,x-1,y);
    if(f) return f;
    p[x-1][y] = p[x][y];p[x][y] = 0;
  }
  if(x<3 && l != 1){
    p[x][y] = p[x+1][y];p[x+1][y] = 0;
    f|=ans(n-1,2,x+1,y);
    if(f) return f;
    p[x+1][y] = p[x][y];p[x][y] = 0;
  }
  if(y == 1 && l != 4){
    p[x][y] = p[x][y-1];p[x][y-1] = 0;
    f|=ans(n-1,3,x,y-1);
    if(f) return f;
    p[x][y-1] = p[x][y];p[x][y] = 0;
  }
  if(y == 0 && l != 3){
    p[x][y] = p[x][y+1];p[x][y+1] = 0;
    f|=ans(n-1,4,x,y+1);
    if(f) return f;
    p[x][y+1] = p[x][y];p[x][y] = 0;
  }
  return f;
}