#include<cstdio>
#include<iostream>
#include<algorithm>
#include<functional>
#include<queue>
#include<cstring>
using namespace std;
short 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}};
short memo[16777216];
class P{
	public:
	int data[8];
	int cnt;
	P(){}
	P(int a[8],int cnt):cnt(cnt){
		for(int i = 0; i < 8; i++){
			data[i] = a[i];
		}
	}
};
int getIndex(int *a){
	int res = 0;
	int r = 1;
	for(int i = 0; i < 8; i++){
		res += a[i]*r;
		r *= 8;
	}
	return res;
}
void output(int *a,int b){
    for(int i = 0;i < 8; i++){
        printf("%d ",a[i]);
    }
    printf(":%d \n",b);
    return ;
}
int main(){
	memset(memo,-1,sizeof(memo));
	queue<P> Q;
	int F[]={0,1,2,3,4,5,6,7};
	Q.push(P(F,0));
	memo[getIndex(F)]=0;
	int cnt = 0;
	while( !Q.empty() ){
		P p = Q.front();Q.pop();
		int zero;
		for(int i = 0; i < 8; i++){
			if(p.data[i] == 0){zero = i;break;}
		}
		for(int i = 0; i < rule[zero][0]; i++){
			swap(p.data[zero],p.data[rule[zero][i+1]]);
			int id = getIndex(p.data);
            if(memo[id] != -1){swap(p.data[zero],p.data[rule[zero][i+1]]);continue;}
            memo[id] = p.cnt+1;
            //output(p.data,memo[id]);
            cnt++;
			Q.push(P(p.data,p.cnt+1));
			swap(p.data[zero],p.data[rule[zero][i+1]]);
		}

	}
	int w=0;
	//for(int i = 0; i < 16777216; i++)if(memo[i] != -1){printf("%d\n",i);w++;}
	//printf("cnt = %d\n",w);
	int in[10];
	while(~scanf("%d%d%d%d%d%d%d%d",&in[0],&in[1],&in[2],&in[3],&in[4],&in[5],&in[6],&in[7])){
		int id = getIndex(in);
		printf("%d\n",memo[id]);
	}
	return 0;
}