#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
string bg,ed="01234567",k1,k2;
int x,y,z,nx,ny,nz;
int a[11];
const int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
void bfs()
{
	if(bg==ed){
		printf("0\n");
		return;
	}
	queue<string>p;
	map<string,int>s;
	p.push(bg);
	s[bg]=0;
	while(!p.empty())
	{
		k1=p.front();
		p.pop();
		for(int i=0;i<8;i++)
		 if(k1[i]=='0')z=i;
		x=z/4,y=z%4;
		for(int i=0;i<4;i++){
			nx=x+dx[i],ny=y+dy[i],nz=nx*4+ny;
			if(nx>=0&&nx<2&&ny>=0&&ny<4&&nz<8){
				k2=k1;
				swap(k2[z],k2[nz]);
				if(!s.count(k2)){
					if(k2==ed){
						printf("%d\n",s[k1]+1);
						return ;
					}
					p.push(k2);
					s[k2]=s[k1]+1;
					swap(k2[z],k2[nz]);
				}
			}
		}
	}

}
int main()
{
	while(scanf("%d",&a[0])!=EOF){
		/*bg="";
		bg+=(char)(a[0]+'0');
		for(int i=1;i<8;i++){
			scanf("%d",&a[i]);
			bg+=(char)(a[i]+'0');
		}
		bfs();*/
		cout<<endl;
	}
	return 0;
}