// 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;

void bfs()
{
	std::queue<std::string> que;
	que.push( "01234567" );

	while( !que.empty() )
	{
		std::string s = que.front(); que.pop();
		int pos = s.find( '0' );

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

	return;
}

int main()
{
	m["01234567"] = 0;
	bfs();

	while( true )
	{
		std::string s;
		rep( i, 8 )
		{
			int a;
			if( scanf( "%d", &a ) == EOF )
				return 0;
			s += a + '0';
		}

		printf( "%d\n", m[s] );
	}

	return 0;
}