#include <bits/stdc++.h>

using namespace std;

struct Puzzle {
    int x;
    string s;
};

map<string, int> hash;
int dx[] = {-1, 1, -4, 4};

void solve() {

    queue<Puzzle> q;
    string start = "01234567";

    hash[start] = 0;
    q.push((Puzzle){0, start});

    while(!q.empty()) {

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

        for(int i = 0; i < 4; ++i) {
            if(now.x + dx[i] >= 0 && now.x + dx[i] < 8 && !(i == 0 && now.x == 4) && !(i == 1 && now.x == 3)) {
                Puzzle next = now;
                next.x += dx[i];
                swap(next.s[now.x], next.s[next.x]);

                if(hash.count(next.s) == 0) {
                    hash[next.s] = hash[now.s] + 1;
                    q.push(next);
                }
            }
        }

    }

}

int main() {

    solve();

    while(1) {

        char num;
        string puzzle = "";

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

        cout << hash[puzzle] << endl;

    }

}