#include <bits/stdc++.h>
using namespace std;
#define mat vector<vector<int> >
const int inf = 0x7f7f7f7f;

int vis[(int)2e7], dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int index(mat m) {
    int res = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) res += (1 << (3 * (i * 4 + j))) * m[i][j];
    }
    return res;
}

int main() {
    mat a(2);
    vector<int> x(4);
    a[0] = a[1] = x;
    memset(vis, 127, sizeof(vis));

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) a[i][j] = i * 4 + j;
    }

    vis[index(a)] = 0;

    queue<mat> que;
    que.push(a);

    while (!que.empty()) {
        mat top = que.front();
        que.pop();
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 4; j++) {
                if (top[i][j] == 0) {
                    for (int k = 0; k < 4; k++) {
                        int nx = i + dx[k], ny = j + dy[k];
                        if (nx >= 0 && nx < 2 && ny >= 0 && ny < 4) {
                            mat tmp = top;
                            swap(tmp[nx][ny], tmp[i][j]);
                            if (vis[index(tmp)] == inf) {
                                que.push(tmp);
                                vis[index(tmp)] = vis[index(top)] + 1;
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

    while (scanf("%d", &a[0][0]) != EOF) {
        for (int i = 1; i < 4; i++) scanf("%d", &a[0][i]);
        for (int i = 0; i < 4; i++) scanf("%d", &a[1][i]);
        int res = vis[index(a)];
        printf("%d\n", res == inf ? -1 : res);
    }
}
