#include"stdio.h"
#include"stdlib.h"
#include"queue"
#include"map"
#include"iostream"
#include"string.h"
#include"algorithm"
using namespace std;
struct mmm
{
	char kk[10];
	int x,sum;
}vv;
char xx[10];
int minn = 0;
char ww[10] = {"01234567"};
int dir[8][3] = {{1,4},{0,2,5},{1,3,6},{2,7},{0,5},{1,4,6},{2,5,7},{3,6}};
void bfs(int x1)
{
	queue <mmm> qq;
	vv.x = x1;
	vv.sum = 0;
	strcpy(vv.kk,xx);
	qq.push(vv);
	map <string,int> mp;
	while(!qq.empty())
	{
		vv = qq.front();
		qq.pop();
		char yy[10];
		strcpy(yy,vv.kk);
		if(mp[yy]) continue;
		mp[yy] ++;
		if(strcmp(ww,yy) == 0)
		{
			minn = vv.sum;
			break;
		}
		int x0 = vv.x;
		int sum0 = vv.sum;
		if(x0 == 0 || x0 == 3 || x0 == 4 || x0 == 7)
		{
			for(int i = 0;i < 2;i ++)
			{
				vv.x = dir[x0][i];
				swap(vv.kk[vv.x],vv.kk[x0]);
				vv.sum = sum0 + 1;
				qq.push(vv);
				strcpy(vv.kk,yy);
			}
		}
		else
		{
			for(int i = 0;i < 3;i ++)
			{
				vv.x = dir[x0][i];
				swap(vv.kk[vv.x],vv.kk[x0]);
				vv.sum = sum0 + 1;
				qq.push(vv);
				strcpy(vv.kk,yy);
			}
		}
	}
}
int main()
{
	int a,b,c,d;
	int pp[10];
	while(~scanf("%d",&pp[0]))
	{
		xx[0] = pp[0] + '0';
		if(!pp[0]) b = 0;
		for(int i = 1;i < 8;i ++)
		{
			scanf("%d",&pp[i]);
			xx[i] = pp[i] + '0';
			if(!pp[i])
			{
				b = i;
			}
		}
		xx[8] = '\0';
		minn = 0;
		bfs(b);
		printf("%d\n",minn);
	}
	return 0;
}