#include<iostream>
#include<map>
#include<queue>
#include<string>

using namespace std;

map<string, int>mymap;
queue<string>que;
int d[] = {1, -1, 4, -4};
int a[10];

void bfs(){
    string cur;
    string nxt;
    que.push("01234567");
    mymap["01234567"] = 0;

    while(que.size()){
        string cur = que.front();
        que.pop();
        int p = (int)cur.find("0");
        for(int i = 0; i < 4; ++i){
            int np = p + d[i];
            if(0 <= np && np < 8 && !(p == 3 && d[i] == 1) && !(p == 4 && d[i] == -1)){
                nxt = cur;
                nxt[np] = '0';
                nxt[p] = cur[np];
                if(mymap.find(nxt) == mymap.end()){
                    mymap[nxt] = mymap[cur] + 1;
                    que.push(nxt);
                }
            }
        }
    }
    return ;
}

int main(){
    bfs();
    while (cin >> a[0]) {
    for (int i = 1; i < 8; ++i) {
      cin >> a[i];
    }
    string s = "";
    for (int i = 0; i < 8; ++i) {
      s += a[i] + '0';
    }
    if (mymap.find(s) == mymap.end())
      cout << -1 << endl;
    else
      cout << mymap[s] << endl;
    }
    return 0;
}

