#include<bits/stdc++.h>
using namespace std;

struct S{
  int co;
  string str;
  int z;
};
map<string,int>cnt;

void dfs(){;
  int d[2][3] = {{-1,1,4},{-1,1,-4}};
  queue<S> que;
  que.push((S){0,"01234567",0});
  while(!que.empty()){
    S s = que.front();que.pop();
    int co = s.co;
    string puzzu = s.str;
    string tmp;
    int z = s.z;
    if(cnt.count(puzzu) != 0)continue;
    cnt[puzzu]=co;
    for(int i = 0; i < 3; i++){
      if(!(z == 3 && z + d[z/4][i] == 4) && !(z == 4 && z + d[z/4][i] == 3) && z + d[z/4][i] >=0 && z + d[z/4][i] < 8){
	tmp = puzzu;
      	swap(tmp[z],tmp[z + d[z/4][i]]);
	que.push((S){co+1,tmp,z+d[z/4][i]});
      }	
    }
  }
}

int main(){
  string imput = "00000000";
  dfs();
  while(cin >> imput[0]){
    for(int i = 1;i < 8;i++){
      cin >> imput[i];
    }
    cout << cnt[imput] << endl;
  }
}