#include <iostream>
#include <queue>
#include <map>

using namespace std;

map<int, int> visited;

typedef struct status {
    int x;
    int y;
    int puzzle[2][4];
} status;


int hash(const int p[2][4])
{
    return ((((((p[0][0] * 8 + p[0][1]) * 8 + p[0][2]) * 8 + p[0][3]) * 8 +
        p[1][0]) * 8 + p[1][1]) * 8 + p[1][2]) * 8 + p[1][3];
}


bool isGoal(const int puzzle[2][4])
{
    for (int i = 0; i < 8; i++) {
        int r = i / 4;
        int c = i % 4;
        if (puzzle[r][c] != i)
            return false;
    }
    return true;
}

void copy(const int src[2][4], int dst[2][4])
{
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
            dst[i][j] = src[i][j];
        }
    }
}
 
int bfs(int puzzle[2][4], int sx, int sy)
{
    static const int dx[] = {-1, 0,  0, 1};
    static const int dy[] = { 0, 1, -1, 0};
    status s;

    visited.clear();
    s.x = sx;
    s.y = sy;
    copy(puzzle, s.puzzle);

    queue<status> q;
    q.push(s);
    while (!q.empty()) {
        status s = q.front();
        q.pop();

        if (isGoal(s.puzzle)) {
            return visited[hash(s.puzzle)];
        }

        for (int i = 0; i < 4; i++) {
            int nx = s.x + dx[i];
            int ny = s.y + dy[i];
            status ns = {nx, ny};
            copy(s.puzzle, ns.puzzle);

            if (nx >= 0 && nx < 4 && ny >= 0 && ny < 2) {
                ns.puzzle[s.y][s.x] = s.puzzle[ny][nx];
                ns.puzzle[ny][nx] = 0;

                if (visited[hash(ns.puzzle)] == 0) {
                    visited[hash(ns.puzzle)] = visited[hash(s.puzzle)] + 1;
                    q.push(ns);
                }
            }
        }
    }
    return -1;
}


int main(int argc, char **argv)
{
    int puzzle[2][4];
    int sx, sy;

    while (!cin.eof()) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 4; j++) {
                cin >> puzzle[i][j];
                if (puzzle[i][j] == 0) {
                    sx = j;
                    sy = i;
                }
            }
        }
        cout << bfs(puzzle, sx, sy) << endl;
    }
}