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

struct data{ int id, count; vector<int> state; };
int vn[] = { -1, 1, -4, 4 };
map<vector<int>, bool> m;

int bfs(int start, vector<int> startstate){

    vector<int> endstate;
    for (int i = 0; i < 8; i++){
        endstate.push_back(i);
    }

    queue<data> que;

    que.push({ start, 0, startstate });

    int ret = -1;

    while (que.size()){

        data p = que.front(); que.pop();

        if (p.state == endstate){
            ret = p.count;
            break;
        }

        for (int i = 0; i < 4; i++){

            int next = p.id + vn[i];

            if (0 <= next && next <= 7){

                vector<int> nextstate = p.state;
                swap(nextstate[p.id], nextstate[next]);

                if (!m[nextstate]){
                    que.push({ next, p.count + 1, nextstate });
                    m[nextstate] = true;
                }
            }
     
        }
    }

    return ret;

}

int main(void){

    vector<int> startstate;
    int start;
    int a[8];
    
    while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]){

        for (int i = 0; i < 8; i++){
            startstate.push_back(a[i]);
            if (a[i] == 0){
                start = i;
            }
        }

        vector<int> v;
        for (int i = 0; i < 8; i++){
            v.push_back(i);
        }
        do{
            m[v] = false;
        } while (next_permutation(v.begin(), v.end()));

        int ans = bfs(start, startstate);

        cout << ans << endl;
    }

}