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

#define ALL(cont) (cont).begin(),(cont).end()

typedef vector<int> state;

template<class T>
void print_vec(vector<T> vec) {
    for (int i = 0;i < 8;i++){
        cout << vec[i] << ' ';
        if(i == 3) cout << endl;
    }
    cout << endl << endl;
}

int main() {
    while(1) {
        state data(8);
        vector<state> visited;
        if(cin.eof()) break;

        for(int i = 0;i < 8;i++){
            cin >> data[i];
        }

        int ans = -1;

        typedef pair<state, int> set;
        queue<set> que;
        que.push(set(data, 0));
        while(!que.empty()) {
            //cout << que.size() << endl;
            set s = que.front();
            que.pop();

            if(ans != -1 && ans <= s.second) continue;

            state sta = s.first;

            if(find(ALL(visited), sta) != visited.end()) continue;
            visited.push_back(sta);

            //judge
            bool comp = true;
            for(int i = 0;i < 8;i++) {
                if(sta[i] != i) {
                    comp = false;
                    break;
                }
            }
            if(comp) {
                ans = min(ans, s.second);
                if(ans == -1) ans = s.second;
                //cout << "comp!" << endl;
                continue;
            }

            //cout << "---------------\n";
            //print_vec(sta);
            //cout << "---------------\n";
            for (std::vector<state>::iterator i = visited.begin(); i != visited.end(); ++i)
            {
                //print_vec(*i);
            }
            //cout << "+++++++++++++++\n";
            
            int zero = 0;
            while(sta[zero] != 0) zero++;

            int dire4[] = {
                zero != 4 ? zero - 1 : -1,
                zero != 3 ? zero + 1 : -1,
                zero - 4,
                zero + 4
            };
            /*
            cout << "+++++++++++++++\n";
            print_vec(sta);
            cout << "+++++++++++++++\n";
            */
            for(int i = 0;i < 4;i++) {
                int pos = dire4[i];
                if(pos < 0 || 8 <= pos) continue;

                state new_sta = sta;
                swap(new_sta[zero], new_sta[pos]);

                //print_vec(new_sta);

                que.push(set(new_sta, s.second + 1));
            }
        }

        cout << ans << endl;
    }

    return 0;
}