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

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};

int BFS(int startState, int finalState) {
    std::map<int, int> step;
    std::queue<int> Q;
    step[startState] = 0;
    Q.push(startState);
    int A[8];
    while(!Q.empty()) {
        int current = Q.front();
        Q.pop();
        int steps = step[current];
        if(current == finalState)
            return steps;
        int x = -1, y = -1;
        for(int i = 7, temp = current; i >= 0; i--) {
            A[i] = temp % 10;
            temp /= 10;
            if(A[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
        }
        for(int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || nx >= 2 || ny < 0 || ny >= 4)
                continue;
            std::swap(A[x * 4 + y], A[nx * 4 + ny]);
            int newValue = 0;
            for(int j = 0; j < 8; j++) {
                newValue = newValue * 10 + A[j];
            }
            if(!step.count(newValue)) {
                step[newValue] = steps + 1;
                Q.push(newValue);
            }
            std::swap(A[x * 4 + y], A[nx * 4 + ny]);
        }
    }
    return -1;
}

int main() {
    const int startState = 1234567;
    int finalState = 0;
    
    while(scanf("%d", &finalState) != EOF) {
        for(int i = 1; i < 8; i++) {
            int t;
            scanf("%d", &t);
            finalState = finalState * 10 + t;
        }
        int step = BFS(startState, finalState);
        printf("%d\n", step);
    }

    return 0;
}