#include <bits/stdc++.h>

#define int             long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )    for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )  FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP              make_pair

using namespace std;
using P = pair<int, int>;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

int dx[] = { 1, 4, -1, -4 };

signed main(){
    map<string, int> ans;
    ans["01234567"] = 1;

    queue<string> que;
    que.push( "01234567" );
    while( !que.empty() ){
        string s = que.front(); que.pop();
        int p0;
        REP( i, 8 ) if( s[i] == '0' ) p0 = i;
        REP( i, 4 ){
            if( 0 <= p0 + dx[i] && p0 + dx[i] < 8 &&
                !( p0 == 3 & i == 0 ) && !( p0 == 4 && i == 2 ) ){
                    string t = s;
                    swap( t[p0], t[p0 + dx[i]] );

                    if( ans[t] == 0 ){
                        ans[t] = ans[s] + 1;
                        que.push( t );
                    }
                }
        }
    }

    while( 1 ){
        string s;
        REP( i, 8 ){
            char c;
            if( !( cin >> c ) ) return 0;
            s += c;
        }

        cout << ans[s] - 1 << endl;
    }
}
