/*
    な〜にがlognは定数じゃ
 */

#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;

typedef pair<long long int, int> Data;
typedef pair<Data, int> Pair;

long long int start;
char hascounted[16777215];
int dx[] = {-1, 1,  0, 0};
int dy[] = { 0, 0, -1, 1};

inline unsigned int calc(long long int t) {
    unsigned int ret = 0;

    for (int i=0; i<8; i++) {
        ret *= 8;
        ret += t & 0xff;
        t /= 0x100;
    }

    return ret;
}

inline char check(long long int t) {
    return hascounted[calc(t)];
}

int main() {
    while (1) {
        queue<Pair> q;
        int pos;

        for (int i=0; i<2; i++) {
            for (int j=0; j<4; j++) {
                int digit;

                if (scanf("%d", &digit) == EOF) return 0;
                if (digit == 0) pos = i*4+j;
                start = start * 0x100 + digit;
            }
        }

        memset(hascounted, 0, sizeof(hascounted));
        q.push(Pair(Data(start, 0), 7-pos));
        while (!q.empty()) {
            Pair p = q.front(); q.pop();
            long long int state = p.first.first;
            int time = p.first.second;
            int pos  = p.second;
            char (*array)[4] = (char (*)[4])&state;
            int i = pos/4;
            int j = pos%4;

            if (state == 0x0001020304050607) {
                printf("%d\n", time);
                break;
            }

            for (int k=0; k<4; k++) {
                int newx = j+dx[k];
                int newy = i+dy[k];

                if (newx < 0 || 4 <= newx || newy < 0 || 2 <= newy) continue;
                swap(array[i][j], array[newy][newx]);
                if (!check(state)) {
                    q.push(Pair(Data(state, time+1), newy*4+newx));
                    hascounted[calc(state)] = 1;
                }
                swap(array[i][j], array[newy][newx]);
            }
        }
    }
}