
#include <map>
#include <string>
#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>

using namespace std;

map<string,int> mp;
int direction[4]={1,-1,4,-4};

void bfs(){
    string now,s;
    queue<string> q;
    mp["01234567"]=0;
    q.push("01234567");
    while(q.size()){
        int index;
        now=q.front();
        q.pop();
        for(int i=0;i<8;i++){
            if(now[i]=='0'){
                index = i; break;
            }
        }
        for(int i=0;i<4;i++){
            int nx = index+direction[i];
            if(0<=nx&&nx<8&&!(index==3&&i==0)&&!(index==4&&i==1)){
                string next = now;
                swap(next[index],next[nx]);
                if(mp.find(next)==mp.end()){
                    mp[next]=mp[now]+1;
                    q.push(next);
                }
            }
        }
    }
}

void solve(){
    string s;
    bfs();
    while(getline(cin,s)){
        s.erase( remove(s.begin(),s.end(),' ') , s.end() );
        printf("%d\n",mp[s]);
    }
}

int main(){
    solve();
    return 0;
}


