#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
int main(){_
    map<string,int> vis;
    map<string,int> step;
    queue<string> qu;
    vis["01234567"] = 1;
    step["01234567"] = 0;
    qu.push("01234567");
    while(!qu.empty()){
        string now = qu.front();
        qu.pop();
        int poi;
        for(int i = 0; i < 8; i++)
            if(now[i] == '0'){
                poi = i;
                break;
            }
        if(poi >= 0 && poi <= 3){ //down
            string next = now;
            int pl = next[poi];
            next[poi] = next[poi + 4];
            next[poi + 4] = pl;
            if(!vis[next]){
                qu.push(next);
                step[next] = step[now] + 1;
                vis[next] = 1;
            }
        }
        if(poi >= 4 && poi <= 7){ //up
            string next = now;
            int pl = next[poi];
            next[poi] = next[poi - 4];
            next[poi - 4] = pl;
            if(!vis[next]){
                qu.push(next);
                step[next] = step[now] + 1;
                vis[next] = 1;
            }
        }
        if(poi % 4 != 0){ //left
            string next = now;
            int pl = next[poi];
            next[poi] = next[poi - 1];
            next[poi - 1] = pl;
            if(!vis[next]){
                qu.push(next);
                step[next] = step[now] + 1;
                vis[next] = 1;
            }
        }
        if(poi % 4 != 3){ //right
            string next = now;
            int pl = next[poi];
            next[poi] = next[poi + 1];
            next[poi + 1] = pl;
            if(!vis[next]){
                qu.push(next);
                step[next] = step[now] + 1;
                vis[next] = 1;
            }
        }
    }
    //
    char ch[20];
    while(gets(ch)){
        for(int i = 0; i <15; i++)
            ch[i] = ch[i*2];
        ch[8] = '\0';
        string str = ch;
        printf("%d\n",step[str]);
    }


    return 0;
}

