#include <iostream>
#include <queue>
#define  SWAP(A,B,C) ((C)=(A),(A)=(B),(B)=(C)) 
#define mp make_pair
using namespace std;
typedef pair<int,int>s;
typedef pair<s,int[2][4]>  P;
//0:上    1: 右   2:下   3:左 
int dx[] = {0,1,0,-1},dy[] = {-1,0,1,0};
int ar[2][4] = {{0,1,2,3},{4,5,6,7}};
int main()
{
	//          方向               &#27493;数 
	while(true)
	{
		P a;a.first.first = -1;a.first.second = 0;
		for(int i = 0;i<2;i++)
			for(int j = 0;j<4;j++)
				cin >> a.second[i][j];
		queue<P> q; q.push(a);
		while(1)
		{
			P b = q.front();q.pop();
			bool flag = false,reach = true;
			int i,j,zx,zy,dir = b.first.first;
			for(i = 0;i<2;i++)	
				for(j = 0;j<4;j++)
				{
					if(ar[i][j]!=b.second[i][j])reach = false;
					if(b.second[i][j]==0){zx = j;zy = i;}
		}
			if(reach)
			{
				cout << b.first.second <<endl;
				break;
			}
			for(int i = 0,k = 2;i<4;i++)
			{
				int nx = zx + dx[i],ny = zy + dy[i],e;
				if(i==2)k *= -1;
				if(nx>=0&&nx<4&&ny>=0&&ny<2&&dir!=i)
				{
					P temp;
					temp.first.first = i+k;temp.first.second = b.first.second + 1;
					for(int i = 0;i<2;i++)	
						for(int j = 0;j<4;j++)
							temp.second[i][j] = b.second[i][j];
					SWAP(temp.second[zy][zx],temp.second[ny][nx],e);
					q.push(temp);
				}
			}
		}
	}
	return 0;
} 