#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define rep(i, n) for(int i = 0 ; i < (int)n ; i++)

typedef pair<string, int> P;

const int dx[] = {1, -1, 4, -4};
map<string, int> ans;


void bfs(){
  queue<P> que;
  set<string> found;
  que.push(P("01234567", 0));  
  while(!que.empty()){   
    P tmp = que.front();
    que.pop();
    
    if(found.find(tmp.first) != found.end()) continue;
    ans[tmp.first] = tmp.second;
    found.insert(tmp.first);
    
    int zero;
    for(int i = 0 ; i < 8 ; i++){
      if(tmp.first[i] == '0') zero = i;
    }
    
    for(int i = 0 ; i < 4 ; i++){
      int nx = zero + dx[i];
      if(nx < 0 || nx > 7 || (zero == 3 && dx[i] == 1) || (zero == 4 && dx[i] == -1)) continue;
      swap(tmp.first[zero], tmp.first[nx]);
      que.push(P(tmp.first, tmp.second+1));
      swap(tmp.first[zero], tmp.first[nx]);      
    }
  }
}

int main(){
  bfs();
  string s;
  while(getline(cin, s)){
    rep(i, s.size()){
      if(s[i] == ' ') s.erase(s.begin()+i);
    }
    cout << ans[s] << endl;
  }
  return 0;
}