#include<iostream>
#include<map>
#include<string>
#include<queue>

std::string s;
int dir[4] = { -1, -4, 1, 4 };
std::map<std::string, int> res;

int main()
{
	int a;
	while( std::cin >> a )
	{
		res.clear();
		s.clear();
		s += a + '0';
		for( int i = 0; i != 7; ++i )
			std::cin >> a, s += a + '0';

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

		while( !que.empty() )
		{
			std::string p = que.front(); que.pop();

			if( p == s )
			{
				std::cout << res[s] << std::endl;
				break;
			}

			int s;
			for( int i = 0; i != 8; ++i )
				if( p[i] == '0' )
					s = i;

			for( int i = 0; i != 4; ++i )
			{
				int ns = s + dir[i];

				if( ns < 0 || ns > 7 || ( s == 3 && i == 2 ) || ( s == 4 && !i ) )
					continue;

				std::string q = p;
				std::swap( q[s], q[ns] );

				if( !res[q] )
				{
					que.push( q );
					res[q] = res[p] + 1;
				}
			}
		}
	}

	return 0;
}