#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); i++ )
#define UREP( i, m, n ) for ( unsigned int i = (unsigned int)( m ); i < (unsigned int)( n ); i++ )
#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = (c).begin(); it != (c).end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define PB( n ) push_back( ( n ) )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define fst first
#define snd second

class comparePuzzle
{
public:
	bool operator()( const pair<int,string> &a, const pair<int,string> &b ) const
	{
		if ( a.fst == b.fst )
		{
			int sa = 0, sb = 0;

			REP( i, 0, 8 )
			{
				sa += abs( a.snd[i] - '0' );
				sb += abs( b.snd[i] - '0' );
			}

			return sa > sb;
		}
		else
		{
			return a.fst > b.fst;
		}
	}
};

int main()
{
	string line;
	while ( getline( cin, line ) )
	{
		if ( line.length() == 0 )
		{
			break;
		}

		string init;
		istringstream iss( line );

		REP( i, 0, 8 )
		{
			int tmp;
			iss >> tmp;
			init += ( tmp + '0' );
		}

		priority_queue< pair<int,string>, vector< pair<int,string> >, comparePuzzle > que;
		que.push( MP( 0, init ) );
		set<string> done;

		while ( !que.empty() )
		{
			pair<int,string> current = que.top();
			que.pop();

			if ( current.snd == "01234567" )
			{
				cout << current.fst << endl;
				break;
			}

			const int si[][2] = {
				0, 4,
				1, 5,
				2, 6,
				3, 7,
				0, 1,
				1, 2,
				2, 3,
				4, 5,
				5, 6,
				6, 7
			};

			REP( i, 0, 10 )
			{
				string next = current.snd;

				if ( next[ si[i][0] ] == '0' || next[ si[i][1] ] == '0' )
				{
					char tmp = next[ si[i][0] ];
					next[ si[i][0] ] = next[ si[i][1] ];
					next[ si[i][1] ] = tmp;

					if ( done.find( next ) != done.end() )
					{
						continue;
					}
					else
					{
						done.insert( next );
						que.push( MP( current.fst + 1, next ) );
					}
				}
			}
		}
	}
	
	return 0;
}