#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <set>
using namespace std;

int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
int tg = 1234567;
struct node
{
	int x,y,step;
	int grid[2][4];
};
set<int>vis;
bool ok (int x,int y){
	if(x<2 && x>=0 && y<4 && y>=0)return true;
	return false;
}
int get(node t){
	int d=10000000;
	int h=0;
	for(int i=0;i<2;i++){
		for(int j=0;j<4;j++){
			h+=t.grid[i][j]*d;
			d/=10;
		}
	}
	return h;
}
int bfs(node s){
	queue<node>Q;
	Q.push(s);
	node w,e;
	while(!Q.empty()){
		w=Q.front();
		Q.pop();
		for(int d=0;d<4;d++){
			e=w;
			e.step++;
			int newx = e.x+dx[d];
			int newy = e.y+dy[d];
			if(ok(newx,newy)){
				swap(e.grid[e.x][e.y],e.grid[newx][newy]);
				e.x=newx;
				e.y=newy;
				int val = get(e);
				if(val==tg)return e.step;
				if(vis.find(val)==vis.end()){
					vis.insert(val);
					Q.push(e);
				}
			}
		}
	}
}
int main(){
	node st;
	while(scanf("%d",&st.grid[0][0])!=EOF){
		vis.clear();
		int x,y;
		st.step=0;
		for(int i=1;i<4;i++)
		{
			scanf("%d",&st.grid[0][i]);
			if(st.grid[0][i]==0)x=0,y=i;
		}
		for(int i=0;i<4;i++)
		{
			scanf("%d",&st.grid[1][i]);
			if(st.grid[1][i]==0)x=1,y=i;
		}
		//printf("%I64d\n",get(st));
		if(get(st)==tg)printf("0\n");
		else 
		printf("%d\n",bfs(st));
	}	
	return 0;
}