#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
 
class SevenPuzzle{
public:
	int p[2][4];
	int n;
	int zero_x,zero_y;

	bool finish(){
		return p[0][0]==0&&p[0][1]==1&&p[0][2]==2&&p[0][3]==3&&
			p[1][0]==4&&p[1][1]==5&&p[1][2]==6&&p[1][3]==7;
	}
	
	void move_right(int x,int y){
		if(zero_x!=3){
			swap(p[y][x],p[y][x+1]);
			zero_x=x+1;
		}
	}	

	void move_left(int x,int y){
		if(zero_x!=0){
			swap(p[y][x],p[y][x-1]);
			zero_x=x-1;
		}
	}	

	void move_up(int x,int y){
		if(zero_y!=0){
			swap(p[y][x],p[y-1][x]);
			zero_y=y-1;
		}
	}	

	void move_down(int x,int y){
		if(zero_y!=1){
			swap(p[y][x],p[y+1][x]);
			zero_y=y+1;
		}
	}	

};

int main(){
	
	for(int a=0;a<1000;a++){
		SevenPuzzle SP;;
		SP.n=0;

		for(int i=0;i<2;i++){
			for(int j=0;j<4;j++){
				cin>>SP.p[i][j];
				if(SP.p[i][j]==0){
					SP.zero_x=j; SP.zero_y=i;
				}
			}
		}

		queue<SevenPuzzle> que;
		que.push(SP);

		while(!que.empty()){
			SP=que.front(); que.pop();

			if(SP.finish()){
				cout<<SP.n<<endl;
				break;
			}
	
			SevenPuzzle next1=SP; next1.move_right(next1.zero_x,next1.zero_y); next1.n++; que.push(next1);//????§????

			SevenPuzzle next2=SP; next2.move_left(next2.zero_x,next2.zero_y); next2.n++; que.push(next2);//????§????

			SevenPuzzle next3=SP; next3.move_up(next3.zero_x,next3.zero_y); next3.n++; que.push(next3);//????§????
	
			SevenPuzzle next4=SP; next4.move_down(next4.zero_x,next4.zero_y); next4.n++; que.push(next4);//????§????
		}
	}

	return 0;
}
	