#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define itrep(it, a) for(it = (a).begin(); it != (a).end(); it++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

int dy[4] = {0,1,0,-1}, dx[4] = {1,0,-1,0};

struct Puzzle{
    int f[8];
    int zero;
    int cnt;

    bool operator < (const Puzzle &p) const {
        for(int i = 0; i < 8; i++){
            if(f[i] == p.f[i]) continue;
            return f[i] > p.f[i];
        }
        return false;
    }
};

bool isTarget(Puzzle p){
    rep(i,8) if(p.f[i] != i) return false;
    return true;
}

int bfs(Puzzle s){
    queue<Puzzle> Q;
    map<Puzzle, bool> V;
    Puzzle u, v;
    s.cnt = 0;
    Q.push(s);
    V[s] = true;

    while(not Q.empty()){
        u = Q.front(); Q.pop();
        if(isTarget(u)) return u.cnt;
        int sy = u.zero / 4;
        int sx = u.zero % 4;
        rep(i,4){
            int tx = sx + dx[i];
            int ty = sy + dy[i];
            if(tx < 0 || ty < 0 || tx >= 4 || ty >= 2) continue;
            v = u;
            swap(v.f[u.zero], v.f[tx + ty * 4]);
            v.zero = tx + ty * 4;
            if(!V[v]){
                V[v] = true;
                v.cnt++;
                Q.push(v);
            }
        }
    }
    return -1;
}

int main(){
    Puzzle inp;
    while(true){
        if(not (cin >> inp.f[0])) break;
        range(i,1,8)cin >> inp.f[i];
        rep(i,9){
            if(inp.f[i] == 0) inp.zero = i;
        }
        cout << bfs(inp) << endl;
    }
}