#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <map>

using namespace std;

map<int, int> dist;

typedef struct status {
    int p[2][4];
    int x;
    int y;
} 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];
}


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];
        }
    }
}


void bfs(void)
{
    status s;

    s.x = s.y = 0;
    for (int i = 0; i < 8; i++) {
        int r = i / 4;
        int c = i % 4;
        s.p[r][c] = i;
    }

    queue<status> q;
    q.push(s);
    dist[hash(s.p)] = 0;

    while (!q.empty()) {
        static const int dx[] = {-1, 0,  0, 1};
        static const int dy[] = { 0, 1, -1, 0};

        status s = q.front();
        q.pop();

        for (int i = 0; i < 4; i++) {
            status ns;
            ns.x = s.x + dx[i];
            ns.y = s.y + dy[i];
            copy(s.p, ns.p);
            if (ns.x >= 0 && ns.x < 4 && ns.y >= 0 && ns.y < 2) {
                ns.p[s.y][s.x] = ns.p[ns.y][ns.x];
                ns.p[ns.y][ns.x] = 0;
                if (dist.count(hash(ns.p)) == 0) {
                    dist[hash(ns.p)] = dist[hash(s.p)] + 1;
                    q.push(ns);
                }
            }
        }
    }
}

int main(int argc, char ** argv)
{
    bfs();

    while (!cin.eof()) {
        int puzzle[2][4];

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 4; j++) {
                cin >> puzzle[i][j];
            }
        }
        if (cin.eof()) {
            break;
        }
        cout << dist[hash(puzzle)] << endl;
    }

    return 0;
}