#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<sstream>
#include<cstdio>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<functional>
#include<iomanip>
#define LL long long
using namespace std;

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

struct node{
    int a[2][4],x,y,step,s;
}t1,t2;

queue<struct node> q;

map<int,int> map_;

bool judge(){
    int x=t2.x,y=t2.y;
    if(x>=0 && x<=1 && y>=0 && y<=3){
        swap(t2.a[x][y],t2.a[t1.x][t1.y]);
        t2.s=0;
        for(int i=0;i<2;i++){
            for(int j=0;j<4;j++){
                t2.s=t2.s*10+t2.a[i][j];
            }
        }
        if(!map_.count(t2.s)) return true;
    }
    return false;
}

void init(){
    for(int i=0;i<2;i++){
        for(int j=0;j<4;j++){
            t1.a[i][j]=4*i+j;
        }
    }
    t1.x=t1.y=t1.step=0;
    t1.s=1234567;
    map_[t1.s]=0;
    q.push(t1);
    while(!q.empty()){
        t1=q.front();
        q.pop();
        t2.step=t1.step+1;
        for(int i=0;i<4;i++){
            t2.x=t1.x+dir[i][0];
            t2.y=t1.y+dir[i][1];
            memcpy(t2.a,t1.a,sizeof(t1.a));
            if(judge()){
                map_[t2.s]=t2.step;
                q.push(t2);
            }
        }
    }
}

int main(void){
    init();
    while(scanf("%d",&t1.a[0][0])!=EOF){
        int i,j;
        t1.s=t1.a[0][0];
        for(i=0;i<2;i++){
            for(j=0;j<4;j++){
                if(i==0 && j==0) continue;
                scanf("%d",&t1.a[i][j]);
                t1.s=t1.s*10+t1.a[i][j];
            }
        }
        printf("%d\n",map_[t1.s]);
    }
    return 0;
}

