#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int main(){
    vector<int> goal {0, 1, 2, 3, 4, 5, 6, 7};
    queue<vector<int>> que;
    que.push(goal);

    map<vector<int>, int> mp;
    mp[goal] = 0;

    while(que.size()){
        vector<int> s = que.front(); que.pop();
        int cost = mp[s];
        int zi = -1;
        for(int i=0;i<8;i++){
            if(s[i] == 0){
                zi = i;
                break;
            }
        }
        if(zi % 4 < 3){
            vector<int> next = s;
            assert(zi + 1 < 8);
            swap(next[zi], next[zi+1]);
            if(mp.find(next) == mp.end()){
                mp[next] = cost + 1;
                que.push(next);
            }
        }
        if(zi % 4 > 0){
            vector<int> next = s;
            swap(next[zi], next[zi-1]);
            assert(zi - 1 >= 0);
            if(mp.find(next) == mp.end()){
                mp[next] = cost + 1;
                que.push(next);
            }
        }

        vector<int> next = s;
        assert((zi+4)%8 >= 0);
        swap(next[zi], next[(zi+4)%8]);
        if(mp.find(next) == mp.end()){
            mp[next] = cost + 1;
            que.push(next);
        }
    }

    int temp;
    while(cin >> temp){
        vector<int> query(8);
        query[0] = temp;
        for(int i=1;i<8;i++){
            cin >> query[i];
        }
        assert(mp.find(query) != mp.end());
        cout << mp[query] << endl;
    }

    return 0;
}