#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int rule[8][5]={{2,1,4},{3,0,2,5},{3,1,3,6},{2,3,7},
                {2,0,5},{3,1,4,6},{3,2,5,7},{2,3,6}};
int vis[8][8][8][8][8][8][8][8];
class P{
    public:
    int a0,a1,a2,a3,a4,a5,a6,a7;
    int cnt;
    P(int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,int cnt):
    a0(a0),a1(a1),a2(a2),a3(a3),a4(a4),a5(a5),a6(a6),a7(a7),cnt(cnt){}
};
bool check(int *x){
    for(int i = 0; i < 8; i++){
        if(x[i] != i)return false;
    }
    return true;
}
void copy(int *y,int *x){
    for(int i = 0; i < 8; i++)y[i] = x[i];
}
int main(){
    int b0,b1,b2,b3,b4,b5,b6,b7;
    while(~scanf("%d%d%d%d%d%d%d%d",&b0,&b1,&b2,&b3,&b4,&b5,&b6,&b7)){
        queue<P> Q;
        Q.push(P(b0,b1,b2,b3,b4,b5,b6,b7,0));
        memset(vis,0,sizeof(vis));
        int f = -1;
        while( !Q.empty() ){
            P p = Q.front();Q.pop();
            int x[10];
            x[0]=p.a0;x[1]=p.a1;x[2]=p.a2;x[3]=p.a3;x[4]=p.a4;
            x[5]=p.a5;x[6]=p.a6;x[7]=p.a7;
            if( check(x) ){
                f = p.cnt;
                break;
            }
            int y[10];
            for(int i = 0; i < 8; i++){
                if(x[i] != 0)continue;
                for(int j = 0; j < rule[i][0]; j++){
                    copy(y,x);
                    swap(y[i],y[rule[i][j+1]]);
                    if(vis[y[0]][y[1]][y[2]][y[3]][y[4]][y[5]][y[6]][y[7]])continue;
                    vis[y[0]][y[1]][y[2]][y[3]][y[4]][y[5]][y[6]][y[7]] = 1;
                    Q.push(P(y[0],y[1],y[2],y[3],y[4],y[5],y[6],y[7],p.cnt+1));
                }
            }
        }
        printf("%d\n",f);
    }
    return 0;
}