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

bool 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(){
  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';

    for(int i=0;i<F;i++)check[i] = false;
    check[code(s)] = true;

    queue<string> q;
    queue<int> n;
    q.push(s);
    n.push(0);
    while(1/*q.size()*/){
      string str = q.front();
      q.pop();
      int tmp = n.front();
      n.pop();

      if(str == "01234567"){
	cout << tmp << endl;
	break;
      }

      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)]){
	  //cout << tmp << ":[" << str << "]" << endl;
	  q.push(str);
	  n.push(tmp+1);
	  check[code(str)] = true;
	}
	swap(str[pos],str[pos+c[i]]);  
      }
    }
  }
}