#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<utility>
using namespace std;

bool can[8][8]={
    0,1,0,0,1,0,0,0,
    1,0,1,0,0,1,0,0,
    0,1,0,1,0,0,1,0,
    0,0,1,0,0,0,0,1,
    1,0,0,0,0,1,0,0,
    0,1,0,0,1,0,1,0,
    0,0,1,0,0,1,0,1,
    0,0,0,1,0,0,1,0,
};



int main(){
    map<vector<int>,int> m;
    vector<int> e={0,1,2,3,4,5,6,7};
     m.insert(make_pair(e,0));
        queue<vector<int>> que;
        que.push(e);

        while(!que.empty()){
            vector<int> p =que.front(); que.pop();
            
            int pos;
            for(int i=0;i<8;i++){
                if(p[i]==0) pos=i;
            }

            for(int i=0;i<8;i++){
                if(can[pos][i]){
                    vector<int> q=p;
                    swap(q[pos],q[i]);

                    if(m.find(q)==m.end()){
                        m.insert(make_pair(q,m[p]+1));
                        que.push(q);
                    }
                    
                }
            }  
        }
    while(true){
        
        vector<int> v(8);
        for(int i=0;i<8;i++){
            cin>>v[i];
        } 

        if(cin.eof()) break;

        cout<<m[v]<<endl;
       
    }
}
