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

using namespace std;

int a[8];
int d[4] = {-1, 1, -4, 4};

map<string, int> mymap;

void bfs() {
    queue<string> q;
    q.push("01234567");
    mymap["01234567"] = 0;
    
    while (!q.empty()) {
        string cur = q.front(); q.pop();
        string next;
        int p = (int)cur.find("0");
        for (int i = 0; i < 4; i++) {
            int np = p + d[i];
            if (np >= 0 && np < 8 && (p != 3 || d[i] != 1) && (p != 4 || d[i] != -1)) {
                next = cur;
                next[np] = '0';
                next[p] = cur[np];
                if (mymap.find(next) == mymap.end()) {
                    mymap[next] = mymap[cur] + 1;
                    q.push(next);
                }
            }
        }
    }
}

int main(int argc, const char * argv[]) {
    bfs();
    while (cin >> a[0]) {
        for (int i = 1; i < 8; i++) {
            cin >> a[i];
        }
        string s = "";
        for (int i = 0; i < 8; i++) {
            s += a[i] + '0';
        }
        if (mymap.find(s) == mymap.end()) {
            cout << -1 << endl;
        } else {
            cout << mymap[s] << endl;
        }
    }
    return 0;
}

