#include<bits/stdc++.h> 
using namespace std;
using i64 = int_fast64_t;
#define rep(i, N) for(int i = 0; i < N; i++)

int main(){   
    using board = vector<vector<int>>;
    board complete = {{0, 1, 2, 3} ,{4, 5, 6, 7}};
    map<board, int> dp;

    queue<pair<board,int>> que;
    que.push({complete, 0}); 
    const int d[4] = {1, -1, 0, 0};
    while(!que.empty()){
        board now;
        int moves;
        int y, x;
        tie(now, moves) = que.front(); 
        que.pop();
        rep(i, 2) rep(k, 4) {
            if(now[i][k] == 0) y = i, x = k;
        } 
        for(int i = 0; i < 4; i++){
            int ny = y + d[i];
            int nx = x + d[i ^ 2];
            if(ny < 0 || ny >= 2 || nx < 0 || nx >= 4) continue;
            swap(now[y][x], now[ny][nx]);
            if(!dp.count(now)){
                dp[now] = moves + 1;
                que.push({now, moves + 1});
            } 
            swap(now[ny][nx], now[y][x]);
        }
    }

    board p(2, vector<int>(4));
    while(cin >> p[0][0] >> p[0][1] >> p[0][2] >> p[0][3] >> p[1][0] >> p[1][1] >> p[1][2] >> p[1][3]){
        cout << dp[p] << endl;
    }                                                                                                                                                                                                                                                                                                                                   
}

