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

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) {
    for(int i=0;i<v.size();i++) {
        if(i != v[i]) return false;
    }
    return true;
}


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

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

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);
        }
        solve();
    }
}