#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(){
    while(true){
        string inp, fir = "";
        if(!getline(cin, inp)) break;
        stringstream ss(inp);
        while(ss >>inp) fir += inp;

        queue<C> open;
        open.push( C(fir, 0) );
        set<string> closed;

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

            if(now.str == "01234567"){
                cout <<now.step <<endl;
                break;
            }

            if(closed.find(now.str) != closed.end()){
                continue;
            }

            closed.insert(now.str);

            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) );
            }
        }
    }
    return 0;
}