// AOJ 0121
#include<map>
#include<queue>
#include<string>
#include<cstdio>
#define rep( i, a ) for( int i = 0; i != (a); ++i )

const int d[4] = { -4, 1, 4, -1 };
std::map<std::string, int> m;

int main()
{
	while( true )
	{
		m.clear();
		std::string s;
		rep( i, 8 )
		{
			int a;
			if( scanf( "%d", &a ) == EOF )
				return 0;

			s += a + '0';
		}

		std::queue<std::string> que;
		que.push( s );

		while( !que.empty() )
		{
			std::string s = que.front(); que.pop();
			int zero;

			bool fl = true;
			for( int i = 0; i != 8; ++i )
			{
				fl &= s[i] - '0' == i;
				if( s[i] == '0' )
					zero = i;
			}

			if( fl )
			{
				printf( "%d\n", m[s] );
				break;
			}

			rep( i, 4 )
			{
				std::string ns = s;
				int nz = zero + d[i];
				
				if( nz >= 0 && nz < 8 && !( zero == 3 && i == 1 ) && !( zero == 4 && i == 3 ) )
				{
					std::swap( ns[zero], ns[nz] );
					
					if( !m[ns] )
					{
						que.push( ns );
						m[ns] = m[s] + 1;
					}
				}
			}
		}
	}

	return 0;
}