#include <iostream>
#include <map>
#include <queue>
#include <cstring>
#include <vector>
#include <cstdio>

#define rep(i,b) for(int i=0; i<int(b); i++)

using namespace std;

map<int,int> ans;
typedef vector<int> vi;

struct state {
    state(int arr_[8], int step)  : step(step) {
        rep(i,8) arr[i] = arr_[i];
    }
    int arr[8];
    int step;
};

int main(){
    queue<state> q;
    int a[8]={0,1,2,3,4,5,6,7};
    q.push({a,0});

    while(q.size()){
        auto s = q.front();
        q.pop();
        int h=0;
        int free;
        int arr[8];

        rep(i,8){
            h|=s.arr[i]<<(i*3);
            if(s.arr[i]==0) free = i;
            arr[i] = s.arr[i];
        }
        // printf("%i\n", h);

        if(ans.find(h)!=ans.end()) continue;
        ans[h] = s.step;

        int other = free;
        if(free<4) other+=4;
        else other-=4;

        swap(arr[free], arr[other]);
        q.push(state(arr, s.step+1));
        swap(arr[free], arr[other]);

        if(free%4!=0){
            swap(arr[free], arr[free-1]);
            q.push(state(arr, s.step+1));
            swap(arr[free], arr[free-1]);
        }
        if(free%4!=3){
            swap(arr[free], arr[free+1]);
            q.push(state(arr, s.step+1));
            swap(arr[free], arr[free+1]);
        }
    }

    while(1){
        int h=0;
        rep(i,8){
            int t;
            if(!(cin>>t)) return 0;
            // cout << t << endl;
            h|=(t<<(i*3));
        }
        cout << ans[h] << endl;
    }
}