#include <bits/stdc++.h>
using namespace std;
int main(void){
    int zero;
    string s="0 1 2 3 4 5 6 7",ans="0 1 2 3 4 5 6 7";
    queue<pair<string,int>> q;
    q.push(make_pair(s,0));
    unordered_map<string,int> m;
    pair<string,int> ps;
    string now;
    int count;
    while(!q.empty()){
        ps=q.front();q.pop();
        now=ps.first;
        count=ps.second;
        m[now]=count;
        zero = now.find('0');
        if((0<=zero&&zero<=4) || (8<=zero&&zero<=12)){
            swap(now[zero],now[zero+2]);
            if(m.find(now) == m.end())
                q.push(make_pair(now,count+1));
            swap(now[zero],now[zero+2]);
        }
        if((2<=zero&&zero<=6) || (10<=zero&&zero<=14)){
            swap(now[zero],now[zero-2]);
            if(m.find(now) == m.end())
                q.push(make_pair(now,count+1));
            swap(now[zero],now[zero-2]);
        }
        if(zero<=6){
            swap(now[zero],now[zero+8]);
            if(m.find(now) == m.end())
                q.push(make_pair(now,count+1));
            swap(now[zero],now[zero+8]);
        }
        if(8<=zero){
            swap(now[zero],now[zero-8]);
            if(m.find(now) == m.end())
                q.push(make_pair(now,count+1));
        }
    }
    while(getline(cin,s)){
        cout<<m[s]<<endl;
    }
}




