#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

map<string, int> dist;

void dfs(string str, int cnt){
  if(cnt > 28) return;
  dist[str] = cnt;
  int pos;
  for(int i=0;i<str.size();i++){
    if(str[i] == '0'){
      pos = i;
      break;
    }
  }
  string tmp;
  if(pos!=0 && pos!=4){
    tmp = str;
    swap(tmp[pos], tmp[pos-1]);
    if(dist.find(tmp)==dist.end()){
      dfs(tmp, cnt+1);
    }else if(dist[str] > cnt+1){
      dfs(tmp, cnt+1);
    }
  }
  if(pos!=7 && pos!=3){
    tmp = str;
    swap(tmp[pos], tmp[pos+1]);
    if(dist.find(tmp)==dist.end()){
      dfs(tmp, cnt+1);
    }else if(dist[str] > cnt+1){
      dfs(tmp, cnt+1);
    }
  }
  tmp = str;
  if(pos<4){
    swap(tmp[pos], tmp[pos+4]);
    if(dist.find(tmp)==dist.end()){
      dfs(tmp, cnt+1);
    }else if(dist[str] > cnt+1){
      dfs(tmp, cnt+1);
    }
  }else{
    swap(tmp[pos], tmp[pos-4]);
    if(dist.find(tmp)==dist.end()){
      dfs(tmp, cnt+1);
    }else if(dist[str] > cnt+1){
      dfs(tmp, cnt+1);
    }
  }
}

main(){
  dfs("01234567", 0);
  string str;
  while(cin >> str){
    char c;
    for(int i=0;i<7;i++){
      cin >> c;
      str += c;
    }
    cout << dist[str] << endl;
  }
  return 0;
}