#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;

int p[8];
int goal = 0,k = 1;
int d[1<<25],tmp,pos;

int toInt(void){
  int res = 0;
  for(int i=7;i>=0;i--){
    res = res*8 + p[i];
  }
  return res;
}

void toVec(int a){
  for(int i=0;i<8;i++){
    p[i] = a%8;
    a/=8;
  }
}


int main(){
  for(int i=0;i<8;i++){
    goal += k*i;
    k *= 8;
  }

  while(cin >> p[0]){
    for(int i=1;i<8;i++)cin >> p[i];
    memset(d,-1,sizeof(d));
    
    int num = toInt();
    d[num] = 0;
    queue<int> q;
    q.push(num);

    while(q.size()){
      num = q.front(); q.pop();
      if(num == goal)break;

      toVec(num);
      pos = -1;
      for(int i=0;i<8;i++){
	if(p[i] == 0){
	  pos = i;
	  break;
	}
      }

      if(pos<4){
	swap(p[pos],p[pos+4]);
	tmp = toInt();
	if(d[tmp]<0){
	  d[tmp] = d[num] + 1;
	  q.push(tmp);
	}
	swap(p[pos],p[pos+4]);
      }else{
	swap(p[pos],p[pos-4]);
	tmp = toInt();
	if(d[tmp]<0){
	  d[tmp] = d[num] + 1;
	  q.push(tmp);
	}
	swap(p[pos],p[pos-4]);
      }

      if(pos%4!=0){
	swap(p[pos],p[pos-1]);
	tmp = toInt();
	if(d[tmp]<0){
	  d[tmp] = d[num] + 1;
	  q.push(tmp);
	}
	swap(p[pos],p[pos-1]);
      }
      if(pos%4!=3){
	swap(p[pos],p[pos+1]);
	tmp = toInt();
	if(d[tmp]<0){
	  d[tmp] = d[num] + 1;
	  q.push(tmp);
	}
	swap(p[pos],p[pos+1]);
      }
    }
    cout << d[goal] << endl;
  }
}