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

using namespace std;

int main(){
    int d[] = {-1,1,-4,4}; //右、左、上、下

    map<string,int> ans;
    ans["01234567"];

    queue< pair<int,string> > que;
    que.push(make_pair(0,"01234567"));

    while(!que.empty()){

        pair<int,string> p(que.front());
        que.pop();

        int k(ans[p.second]);

        for(int i=0;i<4;i++){

            int n(p.first+d[i]);

            if(i==0 && n==3) continue;
            if(i==1 && n==4) continue;

            if(n>=0 && n<8){
                string tmp(p.second);
                tmp[p.first] = tmp[n];
                tmp[n] = '0';

                if(ans.find(tmp) == ans.end()){
                    ans[tmp] = k+1;
                    que.push( make_pair(n, tmp) );
                }
            }
        }
    }

    string s;
    char t;
    while(cin >> t){
        s += t;

        for(int i=0;i<7;i++){
            cin >> t;
            s += t;
        }
        cout << ans[s] << endl;
        s = "";
    }

    return 0;
}
