#include<bits/stdc++.h>

using namespace std;
int F[9] = {1,1,2,6,24,120,720,5040,40320},dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0};
int cantor(string s){
    int l = s.length(),res = 0;
    for(int i = 0;i<l;i++){
        int tot = 0;
        for(int j = i+1;j<l;j++)
            tot += (s[j]<s[i]);
        res += tot*F[l-i-1];
    }
    return res;
}
bool can_move(int x,int y){
    return x>=0&&x<4&&y>=0&&y<4;
}
queue<string>q;
int step[40440];
void bfs(){
    while(!q.empty()){
        string now = q.front();
        q.pop();
        int x,y;
        for(int i = 0;i<8;i++)
            if(now[i]=='0') x = i/4,y = i%4;
        for(int i = 0;i<4;i++){
            if(!can_move(x+dx[i],y+dy[i])) continue;
            int id = (x+dx[i])*4+y+dy[i];
            string b = now;
            swap(b[x*4+y],b[id]);
            if(step[cantor(b)]==-1){
                q.push(b);
                step[cantor(b)] = step[cantor(now)]+1;
            }
        }
    }
}
vector<int>v;
int main(){
    int num;
    while(!q.empty()) q.pop();
    memset(step,-1,sizeof(step));
    step[0] = 0;
    q.push("01234567");
    bfs();
    while(cin>>num){
        v.push_back(num);
        if(v.size()==8){
            int id = 0;
            for(int i = 0;i<=7;i++){
                int cnt = 0;
                for(int j = i+1;j<=7;j++)
                    cnt += (v[j]<v[i]);
                id += F[7-i]*cnt;
            }
            v.clear();
            cout<<step[id]<<endl;
        }
        else continue;
    }
    return 0;
}