#include<cstdio>
//#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
map<string, int> dp;
int d[] = {1, -1, 4, -4};
void bfs()
{
    dp["01234567"] = 0;
    queue<string> que;
    que.push("01234567");
    while(que.size()){
        string now = que.front();  //这里的string用法和python太像了~~~
        que.pop();
        int p;
        for(int i = 0; i < 8; i++){
            if(now[i] == '0'){
                p = i;
                break;
            }
        }
        for(int i = 0; i < 4; i++){
            int np = p + d[i];
            if(np >= 0 && np < 8 && !(p == 3 && i == 0) && !(p == 4 && i == 1)){
                string next = now;
                swap(next[np], next[p]);
                if(dp.find(next) == dp.end()){
                    dp[next] = dp[now] + 1;
                    que.push(next);
                }
            }
        }
    }
}
int main()
{
    bfs();
    int _;
    while(scanf("%d", &_) == 1){
        string s;
        s = '0' + _;
        for(int i = 0; i < 7; i++){
            scanf("%d", &_);
            s += '0' + _;
        }
        printf("%d\n", dp[s]);
    }
    return 0;
}


