#include<iostream>
#include<queue>
#include<algorithm>
#define F 16777216
using namespace std;

int check[F];

int code(string x){
  int k = 1,res = 0;
  for(int i=0;i<8;i++){
    res += (x[i] - '0') * k;
    k *= 8;
  }
  return res;
}

int main(){

  for(int i=0;i<F;i++)check[i] = -1;
  check[code("01234567")] = 0;

  queue<string> q;
  q.push("01234567");

  string str;
  int tmp;
  while(q.size()){
    str = q.front();
    q.pop();
    tmp = check[code(str)];
    
    int pos;
    for(int i=0;i<8;i++){
      if(str[i] == '0')pos = i;
    }
    
    int c[4] = {-4,-1,1,4};
    for(int i=0;i<4;i++){
	if(pos + c[i] < 0 || pos + c[i] > 7 || (pos==3 && c[i]==1) || pos==4 && c[i]==-1 )continue;
	swap(str[pos],str[pos+c[i]]);
	if(check[code(str)]<0){
	  //cout << tmp << ":[" << str << "]" << endl;
	  q.push(str);
	  check[code(str)] = tmp+1;
	}
	swap(str[pos],str[pos+c[i]]);  
      }
    }

  int a[8];
  while(cin >> a[0]){
    for(int i=1;i<8;i++)cin >> a[i];
    string s;
    for(int i=0;i<8;i++)s += a[i]+'0';

    cout << check[code(s)] << endl;
  }
}