#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef stringstream SS;
template < typename T > inline T fromString( const string &s ){ T res; istringstream iss( s ); iss >> res; return res; };
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )
#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

#include <unordered_map>

int encode( const VI &as )
{
	int res = 0;
	FOR( a, as )
	{
		res *= 10;
		res += a;
	}
	return res;
}

VI decode( int m )
{
	VI res;
	REP( i, 0, 8 )
	{
		res.PB( m % 10 );
		m /= 10;
	}
	reverse( ALL( res ) );
	return res;
}


int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	for ( string line; getline( cin, line ); )
	{
		VI as( 8 );
		SS iss( line );
		FOR( a, as )
		{
			iss >> a;
		}

		int n = encode( as );

		queue<int> que;
		que.push( n );

		unordered_map<int,int> costs;
		costs[n] = 0;

		while ( !que.empty() )
		{
			const int m = que.front();
			que.pop();

			VI as = decode( m );

			if ( m == 1234567 )
			{
				cout << costs[m] << endl;
				break;
			}

			const int c = costs[m];

			REP( i, 0, 4 )
			{
				swap( as[i], as[ i + 1 ] );
				const int nh = encode( as );
				if ( !EXIST( costs, nh ) )
				{
					costs[ nh ] = c + 1;
					que.push( encode( as ) );
				}
				swap( as[i], as[ i + 1 ] );
			}
			REP( i, 0, 7 )
			{
				if ( i == 3 )
				{
					continue;
				}

				swap( as[i], as[ i + 1 ] );
				const int nh = encode( as );
				if ( !EXIST( costs, nh ) )
				{
					costs[ nh ] = c + 1;
					que.push( encode( as ) );
				}
				swap( as[i], as[ i + 1 ] );
			}
		}
	}

	return 0;
}