/*
    絶対定数倍改善で通そうな
 */

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

char first[2][4];
map<long long int, int> times;
int dx[] = {-1, 1,  0, 0};
int dy[] = { 0, 0, -1, 1};

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

        for (int i=0; i<2; i++) {
            for (int j=0; j<4; j++) {
                if (scanf("%d", (int*)&first[i][j]) == EOF) return 0;
            }
        }

        times.clear();
        q.push(*(long long int*)&first);
        while (!q.empty() && times.find(506097522914230528) == times.end()) {
            char hasfound = 0;
            char state[2][4];
            long long int tmp = q.front(); q.pop();
            long long int _tmp = tmp;

            memcpy(state, &tmp, sizeof(tmp));
            for (int i=0; i<2; i++) {
                for (int j=0; j<4; j++) {
                    if (state[i][j] == 0) {
                        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(state[i][j], state[newy][newx]);
                            tmp = *(long long int*)state;
                            if (times.find(tmp) == times.end()) {
                                times.insert(make_pair(tmp, times[_tmp]+1));
                                q.push(tmp);
                            }
                            swap(state[i][j], state[newy][newx]);
                        }
                        hasfound = 1;
                        break;
                    }
                }
                if (hasfound) break;
            }
        }
        printf("%d\n", times[506097522914230528]);
    }
}