#include <bits/stdc++.h>

using namespace std;

int main(){
        vector<int>D(8);
        while (cin >> D[0]) {
                for (int i = 1; i < 8; i++) {
                        cin >> D[i];
                }
                map<vector<int>, int>M;
                queue<pair<vector<int>, int>> que;
                que.push(make_pair(D, 0));
                auto ans = D;
                sort(ans.begin(), ans.end());
                M[D] = 1;
                while (que.size()) {
                        auto now = que.front(); que.pop();
                        if (now.first == ans) {
                                cout << now.second << endl;
                                break;
                        }
                        int zero;
                        for (int i = 0; i < 8; i++) {
                                if (now.first[i] == 0) {
                                        zero = i;
                                }
                        }
                        auto E = now.first;
                        if (zero >= 4) {
                                swap(E[zero], E[zero - 4]);
                        } else {
                                swap(E[zero], E[zero + 4]);
                        }
                        if (M[E] != 1) {
                                que.push(make_pair(E, now.second + 1));
                                M[E] = 1;
                        }
                        if (zero - 1 != -1 && zero - 1 != 3) {
                                E = now.first;
                                swap(E[zero], E[zero - 1]);
                                if (M[E] !=1) {
                                        que.push(make_pair(E, now.second + 1));
                                        M[E] = 1;
                                }
                        }
                        if (zero + 1 != 4 && zero + 1 != 8) {
                                E = now.first;
                                swap(E[zero], E[zero + 1]);
                                if (M[E] != 1) {
                                        que.push(make_pair(E, now.second + 1));
                                        M[E] = 1;
                                }
                        }
                }
        }
}