#include <iostream>
#include <string>
#include <queue>
#include <unordered_set>
using namespace std;
string s = "00000000";
string t = "01234567";
int id[8] = {0, -1, 0, 1, -1, 0, 1, 0};
int main() {
    while (true) {
        for (int i = 0; i < 8; ++i) cin >> s[i];
        if (s == "00000000") break;
        int ans = 0;
        queue<pair<string, int>> q;
        q.push({s, 0});
        unordered_set<string> v;
        while (!q.empty()) {
            string ss = q.front().first;
            int step = q.front().second;
            if (ss == t) {
                ans += step;
                s = "00000000";
                break;
            }
            q.pop();
            int p = 0;
            while (p < 8 && ss[p] != '0') ++p;
            swap(ss[p], ss[p + (p < 4 ? 4 : -4)]);
            if (!v.count(ss)) {
                v.insert(ss);
                q.push({ss, step + 1});
            }
            swap(ss[p], ss[p + (p < 4 ? 4 : -4)]);
            if (p % 4 != 3) {
                swap(ss[p], ss[p + 1]);
                if (!v.count(ss)) {
                    v.insert(ss);
                    q.push({ss, step + 1});
                }
                swap(ss[p], ss[p + 1]);
            }
            if (p % 4 != 0) {
                swap(ss[p], ss[p - 1]);
                if (!v.count(ss)) {
                    v.insert(ss);
                    q.push({ss, step + 1});
                }
                swap(ss[p], ss[p - 1]);
            }
        }
        cout << ans << endl;
    }

    return 0;
}

