#include <cstring>
#include <iostream>
#include <cassert>
#include <queue>
#include <map>
#include <string>
#include <tuple>
using namespace std;

char a[9];
typedef tuple<string, int> T;
int d[4] = {1, -1, 4, -4};
map<string, int> sol;

int bfs(string state) {
    if (sol.find(state) != sol.end()) {
        return sol[state];
    }
    else {
        auto Q = queue<T>();
        sol["01234567"] = 0;
        Q.push(make_tuple("01234567", 0));

        string s;
        int x, t;
        while (!Q.empty()) {
            tie(s, t) = Q.front(); Q.pop();
            x = s.find('0');
            for (int i = 0; i < 4; ++i) {
                int xx = x + d[i];
                if (d[i] ==  1 && x == 3) continue;
                if (d[i] == -1 && x == 4) continue;
                if (0 <= xx && xx < 8) {
                    swap(s[x], s[xx]);
                    if (sol.find(s) == sol.end()) {
                        sol[s] = t+1;
                        Q.push(make_tuple(string(s), t + 1));
                    }
                    swap(s[x], s[xx]);
                }
            }
        }
        return sol[state];
    }
}

int main() {
    while (cin >> a[0]) {
        for (int i=1; i<8; ++i) cin >> a[i];
        string s = string(a);
        cout << bfs(s) << endl;
    }
    return 0;
}

