#include<cmath>
#include<cctype>
#include<cstdlib>
#include<cstdio>
#include<climits>

#include<iomanip>
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
#include<string>
#include<queue>
#include<sstream>
#include<map>
#include<set>
#include<utility>
#include<queue>

using namespace std;

int dx[]={1,-1,4,-4};
map<string,int> step;
void bfs(){
  string first = "01234567";
  queue<string> que;
  step[first] = 0;
  que.push(first);
  while(!que.empty()){
    string s = que.front();que.pop();
    int izero = s.find('0');
    for(int i =0;i < 4;i++){
      int nx = izero + dx[i];
      if(0 <= nx && nx < 8)continue;
      if(izero == 3 && i == 0)continue;
      if(izero == 4 && i == 1)continue;
      string ns = s;
      swap(ns[izero],ns[nx]);
      if(step[ns] == 0 || step[ns] + 1 < step[s]){
	step[ns] = step [s] + 1;
	que.push(ns);
      }
    }
  }
}
int main(){
  ios::sync_with_stdio(false);
  bfs();
  string s;
  while(getline(cin, s)){
    s.erase(remove(s.begin(), s.end(),' '),s.end());
    cout << step[s] << endl;
  }
  return 0;
}