#include<iostream>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int dxy[]={-4,-1,1,4};
struct exp{
    string str;
    int cnt,zero;
    exp(string str_,int cnt_,int zero_){
        str=str_;cnt=cnt_;zero=zero_;
    }
};
int main(){
    char  a[9];
    while(cin>>a[0]){
        for(int i=1;i<8;i++)cin>>a[i];
        a[8]=0;
        string pzl=string(a);
        int zerop;
        for(int i=0;i<8;i++)if(pzl[i]=='0')zerop=i;
        exp start(pzl,0,zerop);
        queue<exp>Q;
        map<string,bool>used;
        Q.push(start);
        while(!Q.empty()){
            exp c=Q.front();
            Q.pop();
            if(used[c.str])continue;
            used[c.str]=true;
            if(c.str=="01234567"){
                cout<<c.cnt<<endl;
                break;
            }
            for(int i=0;i<4;i++){
                if(c.zero+dxy[i]<0 || c.zero+dxy[i]>=8)continue;
                exp next(c.str,c.cnt+1,c.zero);
                swap(next.str[c.zero],next.str[c.zero+dxy[i]]);
                if(i==1&&next.zero==3)continue;
                if(i==2&&next.zero==4)continue;
                next.zero=c.zero+dxy[i];
                Q.push(next);
            }

        }

    }

    return 0;
}