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

using namespace std;

int s[8];

int main()
{
    map<string,int> vis;
    queue<string> q;
    queue<int> qstep;

    string target = "01234567";
    q.push(target);
    qstep.push(0);
    vis[target] = 0;
    while(!q.empty()){
        string tmp1 = q.front(); q.pop();
        int step = qstep.front(); qstep.pop();
        for(int i=0;i<8;++i){
            if(tmp1[i]=='0'){
                if(i!=3&&i!=7){
                    string tmp = tmp1;
                    char t = tmp[i];
                    tmp[i] = tmp[i+1];
                    tmp[i+1] = t;
                    if(vis.count(tmp)==0){
                        q.push(tmp);
                        qstep.push(step+1);
                        vis[tmp] = step+1;
                    }
                }
                if(i!=0&&i!=4){
                    string tmp = tmp1;
                    char t = tmp[i];
                    tmp[i] = tmp[i-1];
                    tmp[i-1] = t;
                    if(vis.count(tmp)==0){
                        q.push(tmp);
                        qstep.push(step+1);
                        vis[tmp] = step+1;
                    }
                }
                if(i>3){
                    string tmp = tmp1;
                    char t = tmp[i];
                    tmp[i] = tmp[i-4];
                    tmp[i-4] = t;
                    if(vis.count(tmp)==0){
                        q.push(tmp);
                        qstep.push(step+1);
                        vis[tmp] = step+1;
                    }
                }
                if(i<4){
                    string tmp = tmp1;
                    char t = tmp[i];
                    tmp[i] = tmp[i+4];
                    tmp[i+4] = t;
                    if(vis.count(tmp)==0){
                        q.push(tmp);
                        qstep.push(step+1);
                        vis[tmp] = step+1;
                    }
                }
                break;
            }
        }
    }

    string tmp="";
    while(scanf("%d",&s[0]) != EOF){
        for(int i=1;i<8;++i) scanf("%d",&s[i]);
        tmp = "";
        for(int i=0;i<8;++i) tmp += ('0'+s[i]);
        printf("%d\n",vis[tmp]);
    }
    return 0;
}

