#include <iostream>
#include <queue>
#include <set>
using namespace std;
#define N 4
#define M 2
typedef unsigned int uint;
typedef pair<uint,unsigned int> P;
uint f[N][N];
uint ans_board;


uint calc_board() {
    uint board = 0;
    for(int i=0;i<M;i++) {
        for(int j=0;j<N;j++) {
            board += f[i][j];
            board <<= 3;
        }
    }
    return board >> 3;
}

void set_ans_board() {
    ans_board = 0;
    for(int i=0;i<8;i++) {
        ans_board += i;
        ans_board <<= 3;
    }
    ans_board >>= 3;
}

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

void solve() {
   set<uint> s;
   queue<P> Q;
   uint board = calc_board();
   set_ans_board();
   Q.push(make_pair(board,0));
   while(!Q.empty()) {
       P data = Q.front(); Q.pop();
       uint board = data.first;
       int cost = data.second;
       if(s.find(board) != s.end()) {
           continue;
       }
       s.insert(board);
       if(board == ans_board) {
           printf("%d\n",cost);
           break;
       }

       int zero_pos = 0;
       int mask = 7;
       while(( (board >> (zero_pos * 3)) & mask) != 0) {
           zero_pos++;
       }
       if(zero_pos >= 8) {
           printf("error!");
           exit;
       }

       for(int i=0;i<4;i++) {
           int x = zero_pos + dx[i];
           if(0 <= x && x < 8) {
               uint mask = 7; // 111

               // bit change
               uint a = (board & (mask << (3*x))) >> (3*x);
               uint b = (board & (mask << (3*zero_pos))) >> (3*zero_pos);
               uint xo = a ^ b;
               uint buf = board;
               buf ^= (xo << (3*x));
               buf ^= (xo << (3*zero_pos));
               
               Q.push(make_pair(buf,cost+1));
           }
       }
   }
}

main() {
    while(true) {
        if(cin.eof()) break;
        for(int i=0;i<M;i++) {
            for(int j=0;j<N;j++) {
                cin>>f[i][j];
            }
        }
        solve();
    }
}