#include<cstdio>
#include<iostream>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
typedef pair<string,int>P;
const int INF=1<<16;
string a;
int op[4]={-1,1,-4,4};
map<string,int>dp;
void bfs(){
    queue<P>q;
    q.push(P("01234567",0));
    dp["01234567"]=0;
    while(!q.size()){
        P p=q.front();
        q.pop();
        string s=p.first;
        int cur=p.second;
        for(int i=0;i<4;i++){
            int next=cur+op[i];
            string str=s;
            swap(str[cur],str[next]);
            map<string,int>::iterator it=dp.find(str);
            if(0<=next&&next<8&&!(cur==3&&next==4)&&!(cur==4&&next==3)&&it==dp.end()){
                q.push(P(str,next));
                dp[str]=dp[s]+1;
            }
        }
    }
}
int main(){
    bfs();
    while(getline(cin,a)){
        a.erase(remove(a.begin(),a.end(),' '),a.end());
        cout<<dp[a]<<endl;
    }
    return 0;
}