#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 100000000;
using namespace std;

struct point{ int x, y; };
int dy[4] = {0,1,0,-1}, dx[4] = {1,0,-1,0};
int h = 2, w = 4;
int ans = INF;

bool check(int m[2][4]){
    int k = 0;
    rep(i,h){
        rep(j,w){
            if(m[i][j] != k) return 0;
            k++;
        }
    }
    return 1;
}

void fix(int m[2][4], bool v[2][4]){

}

void dfs(int m[2][4], point before, point p, int cnt){
    if(check(m)){
        ans = min(ans, cnt);
        return;
    }else if(cnt == 30) return;

    rep(i,4){
        point next;
        next.x = p.x + dx[i];
        next.y = p.y + dy[i];
        if(next.x < 0 || next.x >= w || next.y < 0 || next.y >= h) continue;
        if(next.x == before.x && next.y == before.y) continue;

        int cpm[2][4];
        bool cpv[2][4];
        rep(i,2) memcpy(cpm[i], m[i], sizeof(m[i]));
        swap(cpm[p.y][p.x], cpm[next.y][next.x]);
        dfs(cpm, p, next, cnt + 1);
    }

}

int main(){
    int inp;
    int m[2][4];
    while(cin >> m[0][0]){
        ans = INF;
        point zero = point{0,0};

        rep(i,2){
            rep(j,4){
                if(i + j == 0) continue;
                cin >> m[i][j];
                if(m[i][j] == 0) zero = point{j,i};
            }
        }

        dfs(m, zero, zero, 0);

        cout << ans << endl;
    }
}