#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<string>
using namespace std;
const int dy[] = { 1, 0, -1, 0} , dx[] = { 0, 1, 0, -1};
#define fr first
#define sc second
typedef pair < int , int > Pi;
typedef pair < string , Pi > sPi;
string s;
int now;
bool check(string& s){
  for(int i = 0 ; i < 8 ; i++ ){
    if(i + '0' != s[i]) return false;
  }
  return true;
}
int bfs(){
  queue< sPi > que;
  map< string , bool > used;
  que.push(sPi(s,Pi(now,0)));
  used[s] = true;
  while(!que.empty()){
    sPi p = que.front();
    que.pop();
    if(check(p.fr)) return p.sc.sc;
    for(int i = 0 ; i < 4 ; i++ ){
      int ny = p.sc.fr / 4 + dy[i], nx = p.sc.fr % 4 + dx[i];
      string next = p.fr;
      if((ny == 0 || ny == 1) && nx >= 0 && nx < 4){
        swap(next[ny*4+nx],next[p.sc.fr]);
        if(!used[next]){
          used[next] = true;
          que.push(sPi(next,Pi(ny*4+nx,p.sc.sc+1)));
        }
      }
    }
  }
  return -1;
}
int main(){
  s.resize(8);
  while(cin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] >> s[5] >> s[6] >> s[7]){
    now = s.find('0');
    cout << bfs() << endl;
  }
}