#include<iostream>
#include<algorithm>
#include<queue>
#include<climits>
#include<map>
#include<sstream>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<string, int>
const int INF = INT_MAX / 2;

int main(){
  queue<P> open;
  open.push( P("01234567", 0) );
  map<string, int> closed;

  while(!open.empty()){
    int step = open.front().second;
    string str = open.front().first;
    open.pop();

    if(closed.find(str) != closed.end() && closed[str] <= step) continue;
    closed[str] = step;

    int zero;
    REP(i, str.size()) if(str[i] == '0') zero = i;
    int mv[] = {1, -1, 4, -4};

    REP(i, 4){
      if(zero + mv[i] >= str.size()
        || zero + mv[i] < 0
        || (zero == 3 && zero + mv[i] == 4)
        || (zero == 4 && zero + mv[i] == 3)) continue;

      swap(str[zero], str[zero + mv[i]]);
      open.push( P(str, step + 1) );
      swap(str[zero], str[zero + mv[i]]);
    }
  }
  while(true){
    string fir_inp, inp = "";
    if(!getline(cin, fir_inp)) break;
    stringstream ss(fir_inp);
    while(ss >>fir_inp) inp += fir_inp;
    cout <<closed[inp] <<endl;
  }
  return 0;
}