#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
int ques[10];
int dp[50000];
const int rights[9] = {0,5040,720,120,24,6,2,1};
int qtint(int arr[])
{
	int ret(0);
	bool isf[9];
	memset(isf,0,sizeof isf);
	for(int i(1);i<=8;++i)
	{
		int cnt(0);
		for(int j(0);j<arr[i];++j)
		{
			if(!isf[j])	++cnt;	
		}
		isf[arr[i]] = true;
		ret += cnt*rights[i];
	}
	return ret;
}
int main()
{
	memset(dp,-1,sizeof dp);
	queue<int> q,qv;
	q.push(0), qv.push(0);
	dp[0] = 0;
	while(!q.empty())
	{
		int stat(q.front()),step(qv.front());
		q.pop(), qv.pop();
		int astat[10];
		bool isf[9];
		memset(isf,0,sizeof isf);
		for(int i(1);i<8;++i)
		{
			int cnt(stat/rights[i]+1);
			int pos(0);
			for(;cnt;++pos)
			{
				if(!isf[pos])	--cnt;
				if(cnt==0)	break;
			}
			isf[pos] = true;
			stat %= rights[i];
			astat[i] = pos;
		}
		for(int i(0);i<8;++i)
			if(!isf[i])
			{ astat[8] = i; break; }
		int zpx(0),zpy(0);
		for(int i(1);i<=8;++i)
		{
			if(astat[i]==0)
				zpx=(i-1)/4+1, zpy=(i-1)%4+1;
		}
		if(zpx!=1)
		{
			int zpos((zpx-1)*4+zpy),cpos((zpx-2)*4+zpy);
			astat[zpos] = astat[cpos];
			astat[cpos] = 0;
			if(dp[qtint(astat)]==-1)
			{
				dp[qtint(astat)] = step+1;
				q.push(qtint(astat)), qv.push(step+1);
			}
			astat[cpos] = astat[zpos];
			astat[zpos] = 0;
		}
		if(zpx!=2)
		{
			int zpos((zpx-1)*4+zpy),cpos((zpx)*4+zpy);
			astat[zpos] = astat[cpos];
			astat[cpos] = 0;
			if(dp[qtint(astat)]==-1)
			{
				dp[qtint(astat)] = step+1;
				q.push(qtint(astat)), qv.push(step+1);
			}
			astat[cpos] = astat[zpos];
			astat[zpos] = 0;

		}
		if(zpy!=1)
		{
			int zpos((zpx-1)*4+zpy),cpos((zpx-1)*4+zpy-1);
			astat[zpos] = astat[cpos];
			astat[cpos] = 0;
			if(dp[qtint(astat)]==-1)
			{
				dp[qtint(astat)] = step+1;
				q.push(qtint(astat)), qv.push(step+1);
			}
			astat[cpos] = astat[zpos];
			astat[zpos] = 0;

		}
		if(zpy!=4)
		{
			int zpos((zpx-1)*4+zpy),cpos((zpx-1)*4+zpy+1);
			astat[zpos] = astat[cpos];
			astat[cpos] = 0;
			if(dp[qtint(astat)]==-1)
			{
				dp[qtint(astat)] = step+1;
				q.push(qtint(astat)), qv.push(step+1);
			}
			astat[cpos] = astat[zpos];
			astat[zpos] = 0;

		}	
	}
	while(~scanf("%d",ques+1))
	{
		for(int i(2);i<=8;++i)
			scanf("%d",ques+i);
		printf("%d\n",dp[qtint(ques)]);
	}
	return 0;
}

