#include <iostream>
#include <stdio.h>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define N 4
#define M 2
typedef unsigned int uint;
typedef pair<uint,vector<int> > P;
typedef pair<P, int > PP;
vector<int> f;
uint ans_board;
map<vector<int> , int> memo;
int ans_arr[] = {0,1,2,3,4,5,6,7};

int find_zero(vector<int> v) {
    for(int i=0;i<v.size();i++) {
        if(v[i] == 0) return i;
    }
    printf("error!");
}

bool is_same_ans(vector<int> v) {
    if(v.size() != 8) return false;
    for(int i=0;i<v.size();i++) {
        if(i != v[i]) return false;
    }
    return true;
}


int di[]={-1,1,4,-4};
int solve() {
    set<vector<int> > s;
    queue<PP> Q;
    Q.push(PP(P(0, f),find_zero(f)));
    while(Q.size()) {
        PP pp = Q.front(); Q.pop();
        pair<uint,vector<int> > p = pp.first;
        int zero_pos = pp.second;
        uint cost = p.first;
        vector<int> v = p.second;

        if(s.find(v) != s.end()) { // すでに探索済みのボード
            continue;
        }
        if(is_same_ans(v)) {
            return cost;
        }
        s.insert(v);
        for(int j=0;j<4;j++) {
            int i = zero_pos + di[j];
            vector<int> buf_v = v;
            if(0 <= i && i < N*M) {
                swap(buf_v[zero_pos], buf_v[i]);
                Q.push(PP(P(cost + 1, buf_v),i));
            }
        }
    }
}

main() {
    while(true) {
        if(cin.eof()) break;
        f.clear();
        for(int i=0;i<M*N;i++) {
            int d;
            cin>>d;
            f.push_back(d);
        }
        if(memo.find(f) != memo.end()) {
            cout << memo[f] << endl;
        } else {
            memo.insert(pair<vector<int>, int>(f, solve()));
            cout << memo[f] << endl;
        }
    }
}