/*
    クソじゃない？
 */

#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 di[] = {-1, 1, 4};

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

        start = 0;
        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 * 8 + digit;
            }
        }

        memset(hascounted, 0, sizeof(hascounted));
        q.push(Pair(Data(start, 0), 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;

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

            if (pos == 4) di[0] = -100;
            else di[0] = -1;
            if (pos == 3) di[1] = 100;
            else di[1] = 1;
            if (pos >= 4) di[2] = -4;
            else di[2] = 4;

            for (int k=0; k<3; k++) {
                int newpos = pos + di[k];
                long long int mask;
                long long int newstate;
                long long int a;
                long long int b;

                if (newpos < 0 || 8 <= newpos) continue;

                a = 21L-pos*3L;
                b = 21L-newpos*3L;


                mask = (7L << a) | (7L << b);
                newstate = (state & ~mask);
                newstate |= ((state >> a) & 0x7L) << b;
                newstate |= ((state >> b) & 0x7L) << a;
                if (!hascounted[newstate]) {
                    q.push(Pair(Data(newstate, time+1), newpos));
                    hascounted[newstate] = 1;
                }
            }
        }
    }
}