#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int p[8];
bool b[100000000];

bool check(vector<int> v) {
    for(int i = 0; i < 8; i++) {
        if(v[i] != i) return false;
    }
    return true;
}

vector<int> change_place(vector<int> vec, int i, int j) {
    int tmp = vec[i];
    vec[i] = vec[j];
    vec[j] = tmp;
    return vec;
}

int solve() {
    vector<int> v;
    int pos_0;
    for(int i = 0; i < 8; i++) {
        v.push_back(p[i]);
        if(p[i] == 0) pos_0 = i;
    }

    queue<vector<int> > q;
    queue<int> q0;
    queue<int> qc;
    q.push(v);
    q0.push(pos_0);
    qc.push(0);
    int count = 0;
    while(q.size()) {
        vector<int> v_now = q.front(); q.pop();
        pos_0 = q0.front(); q0.pop();
        count = qc.front(); qc.pop();

        if(check(v_now)) break;
        int ix = v_now[0]*10000000
                +v_now[1]*1000000
                +v_now[2]*100000
                +v_now[3]*10000
                +v_now[4]*1000
                +v_now[5]*100
                +v_now[6]*10
                +v_now[7]*1;
        if(b[ix]) continue;
        b[ix] = true;

        if(pos_0-1 >= 0) {
            vector<int> v_next = change_place(v_now, pos_0, pos_0-1);
            q.push(v_next);
            q0.push(pos_0-1);
            qc.push(count+1);
        }
        if(pos_0+1 < 8) {
            vector<int> v_next = change_place(v_now, pos_0, pos_0+1);
            q.push(v_next);
            q0.push(pos_0+1);
            qc.push(count+1);
        }
        if(pos_0-4 >= 0) {
            vector<int> v_next = change_place(v_now, pos_0, pos_0-4);
            q.push(v_next);
            q0.push(pos_0-4);
            qc.push(count+1);
        }
        if(pos_0+4 < 8) {
            vector<int> v_next = change_place(v_now, pos_0, pos_0+4);
            q.push(v_next);
            q0.push(pos_0+4);
            qc.push(count+1);
        }
    }

    cout << count << endl;
    return 0;
}

int main() {
    while(cin >> p[0]) {
        for(int i = 1; i < 8; i++) {
            cin >> p[i];
        }
        solve();
    }
    return 0;
}