#include<stdio.h>
#include<queue>
#include<set>
using namespace std;

const int N = 8;
const int SHIFT_BITS = 2;

unsigned Swap4Bits(unsigned x, int b1, int b2)
{
	unsigned a,b,s;
	b1 <<= SHIFT_BITS;
	b2 <<= SHIFT_BITS;
	a = (x& (0xf<<b1));
	b = (x& (0xf<<b2));
	s = (a>>b1<<b2) | (b>>b2<<b1);
	return (x&~a&~b)|s;
}

int Ans(int initField)
{
	int ans, i, j;
	int zi;
	unsigned field;
	set<unsigned> s;
	queue<unsigned> q;
	unsigned int NEXT_COUNT = 0xffffffff;

	ans = 0;
	q.push(initField);
	q.push(NEXT_COUNT);
	for(;;)
	{
		field = q.front();
		q.pop();
		if(field == NEXT_COUNT)
		{
			++ans;
			q.push(NEXT_COUNT);
			continue;
		}
		if(s.find(field) != s.end())
			continue;
		if(field == 0x01234567)
			break;
		s.insert(field);

		for(i = 0; i < N; ++i)
		{
			if( (field & (0xf<<(i<<SHIFT_BITS))) == 0x00)
			{
				zi = i;
				break;
			}
		}
		
		q.push( Swap4Bits(field, zi, (zi<4?zi+4:zi-4)) );
		if((zi&0x3) != 0x3)
			q.push( Swap4Bits(field, zi, zi+1) );
		if((zi&0x3) != 0x0)
			q.push( Swap4Bits(field, zi,zi-1) );
	}
	return ans;
}

int main(void)
{
	int a[N], i, j;

	for(;;)
	{
		unsigned field = 0;
		for(i = 0; i < N; ++i)
		{
			if(scanf("%d",&a[i])!=1) return 0;
			field |= (a[i] << ((N-i-1)<<SHIFT_BITS));
		}
		printf("%d\n",Ans(field));
	}
	return 1;
}