//21:46~
//??????????????????????????£???
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <map>
#include <stdio.h>

using namespace std;

string data;
char goal[9] = "01234567";
queue<string> qu;
map<string,int> mp;
int d[4] = {-4,-1,1,4};
int pos;
int ans[1000];
char tmp;


int solve(){
  mp[data] = 0;
  qu.push(data);
  string pre_data;
  while(qu.size()){
    data = qu.front();
    pre_data = data;
    qu.pop();
    if(data == goal)
      return mp[data];
    pos = data.find('0');
    for(int i = 0; i < 4; i++){
      if(pos + d[i] >= 0 && pos + d[i] < 8){
        swap(data[pos],data[pos+d[i]]);
        if(mp.find(data) == mp.end()){
          mp[data] = mp[pre_data] + 1;
          qu.push(data);
        }
      }
    }
  }
  return mp[goal];
}

int main(){
  int n = 0;
  size_t c;
  while(getline(cin, data)){
    while((c = data.find_first_of(" ")) != string::npos){
      data.erase(c,1);
    }
    ans[n++] = solve();
    mp.clear();
  }
  for(int i = 0; i < n; i++)
    printf("%d\n", ans[i]);
}