#include <iostream>
#include <set>
#include <queue>
using namespace std;

unsigned long long int swapPos( unsigned long long int s, int a, int b )
{
	unsigned long long int x, y, ret;
	x = ( s >> ( ( 7 - a ) * 8 ) ) & ( ( 1 << 8 ) - 1 );
	y = ( s >> ( ( 7 - b ) * 8 ) ) & ( ( 1 << 8 ) - 1 );
	unsigned long long int mask;
	mask = ( ( ( 1ULL << 8 ) - 1 ) << ( ( 7 - a ) * 8 ) ) | ( ( ( 1ULL << 8 ) - 1 ) << ( ( 7 - b ) * 8 ) );
	ret = s & ~( mask );
	x = x - a + b;
	y = y - b + a;
	ret += ( x << ( ( 7 - b ) * 8 ) ) + ( y << ( ( 7 - a ) * 8 ) );
	return ret;
}

int main()
{

	int a[ 8 ];
	while( cin >> a[ 0 ] ){
		for( int i = 1; i < 8; ++i ){
			cin >> a[ i ];
		}

		set< unsigned long long int > path;
		unsigned long long int ansPattern = 0;
		queue< unsigned long long int > q;
		unsigned long long int s = 0;
		for( int i = 0; i < 8; ++i ){
			s <<= 8;
			s += ( a[ i ] << 4 );
			s += i;
			ansPattern <<= 8;
			ansPattern += ( i << 4 );
			ansPattern += i;
		}
		path.insert( s );
		q.push( s );

		int ans = 0;
		bool found = false;
		while( 1 ){
			int size = q.size();
			for( int i = 0; i < size; ++i ){
				unsigned long long int s = q.front();
				q.pop();
				if( s == ansPattern ){
					found = true;
					break;
				}
				int pos = 0;
				unsigned long long int t = s;
				for( int i = 0; i < 8; ++i ){
					if( ( ( t & ( ( 1 << 8 ) - 1 ) ) ) - ( 7 - i ) == 0 ){
						pos = 7 - i;
						break;
					}
					t >>= 8;
				}
				bool right, left;
				right = left = false;
				if( pos != 3 && pos != 7 ) right = true;
				if( pos != 0 && pos != 4 ) left = true;
				t = swapPos( s, pos, ( pos + 4 ) % 8 );
				if( path.find( t ) == path.end() ){
					path.insert( t );
					q.push( t );
				}
				if( right ){
					t = swapPos( s, pos, pos + 1 );
					if( path.find( t ) == path.end() ){
						path.insert( t );
						q.push( t );
					}
				}
				if( left ){
					t = swapPos( s, pos, pos - 1 );
					if( path.find( t ) == path.end() ){
						path.insert( t );
						q.push( t );
					}
				}
			}
			if( found ) break;
			++ans;
		}
		cout << ans << endl;
	}

	return 0;
}