#include<cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
using namespace std;
union Board{
	unsigned long long ull;
	char b[8];
	bool operator<(const Board& rhs) const{return ull<rhs.ull;}
};
map<Board,int> done;
int* goalstep;

void walk(int step,Board b,int z){
	Board nb;
	int nz;
	int& s=done[b];
	if(!s || s>step){
		s=step;
		if(step<*goalstep){
			// move 0 up/down
			nz=z^4;
			nb.ull=b.ull;
			swap(nb.b[z],nb.b[nz]);
			walk(step+1,nb,nz);

			if(z%4!=0){
				// move 0 left
				nz=z-1;
				nb.ull=b.ull;
				swap(nb.b[z],nb.b[nz]);
				walk(step+1,nb,nz);
			}

			if(z%4!=3){
				// move 0 right
				nz=z+1;
				nb.ull=b.ull;
				swap(nb.b[z],nb.b[nz]);
				walk(step+1,nb,nz);
			}
		}
	}
}
int main(){
	int i,v,z;//,lb;
	Board b,goal;
	b.ull=0;
	for(i=0;i<8;i++)
		goal.b[i]=i;
	for(i=0;~scanf("%d",&v);){
		b.b[i]=v;
		if(v==0)
			z=i;
		//else
			//lb+=abs(i%4-v%4)+abs(i/2-v/2);
		if(++i==8){
			done[goal]=30;
			goalstep=&done[goal];
			walk(1,b,z);
			printf("%d\n",*goalstep-1);
			done.clear();
			b.ull=i=0;
		}
	}
	return 0;
}