#include <bits/stdc++.h>
using namespace std;
int main(void){
    vector<int> s(8);
    int d[4]={-1,1,-4,4};
    queue<vector<int>> que;
    que.push({0,1,2,3,4,5,6,7});
    map<vector<int>,int> arrived;
    arrived[{0,1,2,3,4,5,6,7}]=0;
    while(!que.empty()){
        vector<int> now=que.front();
        que.pop();
        int pos=find(now.begin(),now.end(),0)-now.begin();
        for(int i=0;i<4;i++){
            if((pos+d[i]<8)&&(pos+d[i]>=0)){
                if(pos==3&&pos+d[i]==4){
                    continue;
                }
                if(pos==4&&pos+d[i]==3){
                    continue;
                }
                vector<int> tmp=now;
                swap(tmp[pos],tmp[pos+d[i]]);
                if(arrived.find(tmp)==arrived.end()){
                    que.push(tmp);
                    arrived[tmp]=arrived[now]+1;    
                }
            }
        }
    }
    while(cin>>s[0]){
        for(int i=1;i<8;i++){
            cin>>s[i];
        }
        cout<<arrived[s]<<endl;
    }
}

