#include <bits/stdc++.h>

using namespace std;

struct Puzzle {
    int x;
    string s;
};

int vx[] = {1, -1, 4, -4};
map<string, int> res;

void solve() {

    queue<Puzzle> q;

    q.push((Puzzle){0, "01234567"});

    while(!q.empty()) {

        Puzzle now = q.front(); q.pop();

        for(int i = 0; i < 4; ++i) {
            if(now.x + vx[i] >= 0 && now.x + vx[i] < 8 && !(now.x == 3 && i == 0) && !(now.x == 4 && i == 1)) {

                Puzzle next = now;
                next.x = now.x + vx[i];
                swap(next.s[now.x], next.s[next.x]);

                if(!res[next.s]) {
                    q.push(next);
                    res[next.s] = res[now.s] + 1;
                }

            }
        }

    }

}

int main() {

    res["01234567"] = 1;

    solve();

    while(1) {

        char num;
        string start;

        for(int i = 0; i < 8; ++i) {
            if(!(cin >> num)) return 0;
            start += num;
        }

        cout << res[start] - 1 << endl;

    }

}