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

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

int di[]={-1,1,4,-4};
void preset_map() {
    queue<PP> Q;
    set<vector<int> > s;
    vector<int> ans_f;
    for(int i=0;i<N*M;i++) ans_f.push_back(i);
    
    Q.push(PP(P(0,ans_f),0));
    while(Q.size()) {
         PP pp = Q.front(); Q.pop();
         vector<int> v = pp.first.second;
         int cost = pp.first.first;
         int zerop = pp.second;
       if(s.find(v) != s.end()) {
           continue;
       }
       //for(int i=0;i<8;i++) {
       //    printf("%d ", v[i]);
       //}
       //printf("\n cost=%d-----------\n", cost);
       m.insert(pair<vector<int>, int>(v, cost));
       s.insert(v);

       for(int i=0;i<4;i++) {
           vector<int> buf_f = v;
           int pos = zerop + di[i];
           if(pos == 4 && zerop == 3 ||
              pos == 3 && zerop == 4) {
               continue;
           }
           if(0 <= pos && pos <= 7) {
               swap(buf_f[zerop], buf_f[pos]);
               Q.push(PP(P(cost+1,buf_f), pos));
           }
       }
    }
}

main() {
    preset_map();
    while(true) {
        if(cin.eof()) break;
        f.clear();
        for(int i=0;i<M*N;i++) {
            int d;
            cin>>d;
            f.push_back(d);
        }
        cout << m[f] << endl;
    }
}