#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<string.h>
#include<functional>
#include<set>
#include<string>
#include<bitset>
//aoj 0121
using namespace std;
char zzzt[8]={0,1,2,3,4,5,6,7};
char bj[8];
bitset<90000000> vis;
struct pt{
	int ztt;
	int step;
};
int bs;
pt dl[1<<16];
int front=1,back=1;
int qy=1<<16;
int tt;
void bfs()
{
	tt=0;
	front=1;
	back=1;
	bs=0;
	vis.reset();
	int cs=atoi(bj);
	vis[cs]=1;
	pt lsss;
	lsss.ztt=cs;
	lsss.step=0;
	dl[back&qy]=lsss;
	back++;
	while(front<back)
	{
		pt ls1=dl[front&qy];
		front++;
		if(ls1.ztt==1234567)
		{
			bs=ls1.step;
			tt=1;
			return;
		}
		char ls[8];
		sprintf(ls,"%08d",ls1.ztt);
		int lwz=strchr(ls,'0')-ls;
		pt lsjr;
		switch(lwz)
		{
			case 0:
				swap(ls[0],ls[1]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[1],ls[0]);
				swap(ls[0],ls[4]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				break;
			case 1:
			case 2:
				swap(ls[lwz],ls[lwz+1]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[lwz],ls[lwz+1]);
				swap(ls[lwz],ls[lwz-1]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[lwz],ls[lwz-1]);
				swap(ls[lwz+4],ls[lwz]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				break;
			case 3:
				swap(ls[lwz+4],ls[lwz]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[lwz+4],ls[lwz]);
				swap(ls[lwz-1],ls[lwz]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				break;
			case 4:
				swap(ls[4],ls[5]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[4],ls[5]);
				swap(ls[0],ls[4]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				break;
			case 5:
			case 6:
				swap(ls[lwz],ls[lwz+1]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[lwz],ls[lwz+1]);
				swap(ls[lwz],ls[lwz-1]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[lwz],ls[lwz-1]);
				swap(ls[lwz-4],ls[lwz]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				break;
			case 7:
				swap(ls[lwz-4],ls[lwz]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				swap(ls[lwz-4],ls[lwz]);
				swap(ls[lwz-1],ls[lwz]);
				lsjr.ztt=atoi(ls);
				if(!vis[lsjr.ztt])
				{
					lsjr.step=ls1.step+1;
					vis[lsjr.ztt]=1;
					dl[back&qy]=lsjr;
					back++;
				}
				break;
		}
		
	}
}
int main()
{
	qy--;
	while(1)
	{
		if(scanf("%c",&bj[0])==EOF)
		{
			return 0;
		}
		scanf("%*c");
		for(int i=1;i<=6;i++)
		{
			scanf("%c%*c",&bj[i]);
		}
		scanf("%c",&bj[7]);
		bfs();
		if(tt)
		{
			printf("%d\n",bs);
		}else{
			printf("-1\n");
		}
		while(getchar()!='\n');
	}
	return 0;
}







