#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<queue>
#include<utility>
using namespace std;
int dx[4]={-1,0,1,0}; int dy[4]={0,1,0,-1};
int plane[2][4]={0,1,2,3,4,5,6,7}; //????????×´?¬
int state[2][4]; //×´?¬??±?

int pre[8];
int back_contor[8];
int factorial[8]={5040,720,120,24,6,2,1,1};
int tag[100000];
typedef struct{
    int x,y;
    int n;
    int step;
}p;

void PreCantor()
{
    memset(pre,0,sizeof(pre));
    for(int i=0;i<2;++i){
        for(int j=0;j<4;++j){
            int k=4*i+j;
            for(int l=0;l<k;++l){
                int ni=l/4;int nj=l%4;
                if(state[ni][nj]<state[i][j])pre[k]++;
            }
        }
    }
}

int CantorExpansion()
{
    PreCantor();
    int ans=0;
    for(int i=0;i<2;++i){
        for(int j=0;j<4;++j){
            int k=4*i+j;

            ans+=(state[i][j]-pre[k])*factorial[k];
        }
    }
    return ans;
}

int BackCantor(int n)
{
    memset(back_contor,0,sizeof(back_contor));
    int m;
    for(int i=0;i<8;++i){
        m=n/factorial[i];
        n=n%factorial[i];
        int cnt=0;
        int j;
        for(j=0;j<8&&cnt<=m;++j){
            if(back_contor[j]==0)++cnt;
        }back_contor[j-1]=1;state[i/4][i%4]=j-1;

    }


}

int bfs(int x,int y)
{
    queue<p> que;
    int target=CantorExpansion();
    for(int i=0;i<2;++i){
        for(int j=0;j<4;++j){
            state[i][j]=plane[i][j];
        }
    }
    p first={x,y,CantorExpansion(),0};
    que.push(first);
    while(que.size()){
        p now=que.front();
        que.pop();
        tag[now.n]=0;
        if(now.n==target)return now.step;
        for(int i=0;i<4;++i){
            BackCantor(now.n);

            int nx=now.x+dx[i]; int ny=now.y+dy[i];
            if(nx>=0&&nx<4&&ny>=0&&ny<4){
                state[now.x][now.y]=state[nx][ny];
                state[nx][ny]=0;
                int nn=CantorExpansion();
                if(tag[nn]==-1){
                    p newn={nx,ny,nn,now.step+1};
                    que.push(newn);
                }

            }

        }

    }
    return -1;
}

int main()
{
    int x=0,y=0;
    bool contiorend=true;
    while(contiorend){
        memset(pre,0,sizeof(pre));
        memset(back_contor,0,sizeof(back_contor));
        memset(tag,-1,sizeof(tag));
        for(int i=0;i<2;++i){
            for(int j=0;j<4;++j){
                if(!(cin>>state[i][j])){
                    contiorend=false;
                    break;
                }
            }
        }
        if(contiorend)cout<<bfs(x,y)<<endl;
    }
}