#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;
int A[8];
int dz[4] = {-4,-1,1,4};
map<string,int> d;

void bfs() {
    d["01234567"] = 0;
    queue<string> Q;
    Q.push("01234567");
    while(!Q.empty()) {
        string cur = Q.front(); Q.pop();
        int z;
        for(int i=0; i<8; i++) {if(cur[i] == '0') {z = i; break;}}
        for(int i=0; i<4; i++) {
            int nz = z + dz[i];
            if(z == 3 && nz == 4) continue;
            if(z == 4 && nz == 3) continue;
            if(nz >= 0 && nz < 8) {
                string ns = cur;
                swap(ns[z], ns[nz]);
                if(d.find(ns) == d.end()) {
                    d[ns] = d[cur] + 1;
                    Q.push(ns);
                }  
            }
        }
    }
}

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