#include <cstdio>
#include <queue>
#include <set>

using namespace std;

const int LEN = 8;
int pos;
typedef pair<vector<int>, int> P;
queue<P> q;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int result;
set<vector<int> > reached;

int main() {
    while (1) {
        vector<int> puzzle(LEN);
        queue<P> empty_q;
        swap(q, empty_q);
        reached.clear();
        for (int i = 0; i < LEN; i++) {
            int ret = scanf("%d", &puzzle[i]);
            if (ret < 0)
                goto finish;
            if (puzzle[i] == 0)
                pos = i;
        }
        q.push(make_pair(puzzle, 0));
        reached.insert(puzzle);
        result = -1;
        while (1) {
            P& node = q.front();
            vector<int> arr = node.first;
            int time = node.second;
            q.pop();
            bool good = true;
            for (int i = 0; i < LEN; i++) {
                if (arr[i] != i)
                    good = false;
                if (arr[i] == 0)
                    pos = i;
            }
            if (good) {
                result = time;
                break;
            }
            for (int j = 0; j < 4; j++) {
                int x = pos/4 + dx[j];
                int y = pos%4 + dy[j];
                if (x >= 0 && x < 2 && y >= 0 && y < 4) {
                    vector<int> arr2 = arr;
                    swap(arr2[pos], arr2[4*x+y]);
                    if (reached.find(arr2) == reached.end()) {
                        q.push(make_pair(arr2, time+1));
                        reached.insert(arr2);
                    }
                }
            }
        }
        printf("%d\n", result);
    }
finish:
    return 0;
}