#include<stdio.h>
#include<queue>
using namespace std;
struct node{
	int sz[8];
	int step;
	int order;
	int place;
};
typedef struct node Node;
queue<Node>Q;
//int bz[][2]={{1,0},{-1,0},{0,1},{0,-1}};
int bfs(Node vn){
	Node copy;
	while(!Q.empty()){
		
		int P=Q.front().place;
		int der=Q.front().order;
		
		if(P<4){
		 for(int i=-1;i<=1;i++){
		 	if(i!=0&&P+i>=0&&P+i<4&&der!=i){
		 		copy=Q.front();
		 		copy.order =-i;
		 		copy.place =P+i;
		 		copy.step +=1;
		 		copy.sz[P]=copy.sz[P+i];
		 		copy.sz[P+i]=0;
		 		for(int j=0;j<8;j++){
			if(copy.sz[j]!=j)break;
			if(j==7)return copy.step;
		}
		 		Q.push(copy);
			 }
		}
			 if(der!=2){
			 	copy=Q.front();
			 	copy.order=2;
			 	copy.place=P+4;
			 	copy.sz[P]=copy.sz[P+4];
			 	copy.sz[P+4]=0;
			 	copy.step+=1;
			 for(int j=0;j<8;j++){
			if(copy.sz[j]!=j)break;
			if(j==7)return copy.step;
		}
			 	Q.push(copy);
			 }
		 }
		 
		else{
			for(int i=-1;i<=1;i++){
		 	if(i!=0&&P+i>=4&&P+i<8&&der!=i){
		 		copy=Q.front();
		 		copy.order =-i;
		 		copy.place =P+i;
		 		copy.step +=1;
		 		copy.sz[P]=copy.sz[P+i];
		 		copy.sz[P+i]=0;
		 	for(int j=0;j<8;j++){
			if(copy.sz[j]!=j)break;
			if(j==7)return copy.step;
		}
		 		Q.push(copy);
			 }
		}
			 if(der!=2){
			 	copy=Q.front();
			 	copy.order=2;
			 	copy.place=P-4;
			 	copy.sz[P]=copy.sz[P-4];
			 	copy.sz[P-4]=0;
			 	copy.step+=1;
			for(int j=0;j<8;j++){
			if(copy.sz[j]!=j)break;
			if(j==7)return copy.step;
		}
			 	Q.push(copy);
			 }
		 }
		Q.pop();
	}
	return 0;
}
int main(){
	Node vb;
 while(scanf("%d%d%d%d%d%d%d%d",&vb.sz[0],&vb.sz[1],&vb.sz[2],&vb.sz[3],&vb.sz[4],&vb.sz[5],&vb.sz[6],&vb.sz[7])!=EOF){
 	//getchar();
 	while(!Q.empty())Q.pop();
 	for(int i=0;i<8;i++){
 		if(vb.sz[i]==0){
 			vb.place=i;vb.step=0;vb.order =0;
 			Q.push(vb);
 			printf("%d\n",bfs(vb));
 			break;
		 }
	 }
 }
} 