#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
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++)
const int INF = INT_MAX / 2;

class C{
    public:
        string str;
        int step;
        C(string _str, int _step);
};

C::C(string _str, int _step){
    str = _str;
    step = _step;
}

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

    while(!open.empty()){
        C now = open.front();
        open.pop();

        if(ans.find(now.str) != ans.end() && ans[now.str] <= now.step){
            continue;
        }

        ans[now.str] = now.step;

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

        REP(i, 4){
            if(zero + mv[i] >= now.str.size()) continue;
            if(zero + mv[i] < 0) continue;
            if(zero == 3 && zero + mv[i] == 4) continue;
            if(zero == 4 && zero + mv[i] == 3) continue;
            string tmp = now.str;
            swap(tmp[zero], tmp[zero + mv[i]]);
            open.push( C(tmp, now.step + 1) );
        }
    }
    while(true){
        string fir_inp, inp = "";
        if(!getline(cin, fir_inp)) break;
        stringstream ss(fir_inp);
        while(ss >>fir_inp) inp += fir_inp;
        cout <<ans[inp] <<endl;
    }
    return 0;
}