#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

constexpr int dy[] = { 0, 1, 0, -1 };
constexpr int dx[] = { 1, 0, -1, 0 };

#include <unordered_map>

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

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

		queue< VI > que;
		que.push( org );

		map< VI, int > costs;
		costs[ org ] = 0;

		while ( !que.empty() )
		{
			VI as = que.front();
			que.pop();

			const int c = costs[ as ];

			if ( is_sorted( ALL( as ) ) )
			{
				cout << c << endl;
				break;
			}

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