#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int INF=10000000;

class SP{
public:
	int p[4][6];
	int n;
	int zx,zy;
	
	bool is_over(){
		return p[1][1]==0&&p[1][2]==1&&p[1][3]==2&&p[1][4]==3&&p[2][1]==4&&p[2][2]==5&&p[2][3]==6&&p[2][4]==7;
	}
	
	void swap1(int x,int y){//ue
		if(p[y-1][x]!=INF){
			swap(p[y][x],p[y-1][x]);
			zy--;
		}
	}
	void swap2(int x,int y){//sita
		if(p[y+1][x]!=INF){
			swap(p[y][x],p[y+1][x]);
			zy++;
		}
	}
	void swap3(int x,int y){//migi
		if(p[y][x+1]!=INF){
			swap(p[y][x],p[y][x+1]);
			zx++;
		}
	}
	void swap4(int x,int y){//hidari
		if(p[y][x-1]!=INF){
			swap(p[y][x],p[y][x-1]);
			zx--;
		}
	}	
	
};


	

int main(){
	SP now;
	for(int y=0;y<=3;y++){
		for(int x=0;x<=5;x++){
			now.p[y][x]=INF;
		}
	}
	
	for(int y=1;y<=2;y++){
		for(int x=1;x<=4;x++){
			cin>>now.p[y][x];
			if(now.p[y][x]==0){
				now.zx=x; now.zy=y;
			}
		}
	}
	now.n=0;

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

	while(!que.empty()){
	
		now=que.front(); que.pop();
		if(now.is_over()){
			cout<<now.n<<endl;
			break;
		}
		
		SP nex1=now; nex1.swap1(now.zx,now.zy); nex1.n++; que.push(nex1);
		SP nex2=now; nex2.swap2(now.zx,now.zy); nex2.n++; que.push(nex2);
		SP nex3=now; nex3.swap3(now.zx,now.zy); nex3.n++; que.push(nex3);
		SP nex4=now; nex4.swap4(now.zx,now.zy); nex4.n++; que.push(nex4);
	}
	return 0;
		
}