#include <iostream>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

int a[8] = {0,1,2,3,4,5,6,7};
unsigned char result[76543211];

int conv_n(vector<int> a) {
    int retval = 0;
    for(int i = 0;i < a.size();i++) {
        retval = retval * 10 + a[i];
    }
    return retval;
}

void solve() {
    for(int i = 0;i < 76543211;i++) {
        result[i] = 100;
    }
    queue<pair<vector<int>,int> > bfs;
    vector<int> temp;
    for(int i = 0;i < 8;i++) {
        temp.push_back(a[i]);
    }
    bfs.push(make_pair(temp,0));
    while(!bfs.empty()) {
        temp = (bfs.front()).first;
        int depth = (bfs.front()).second;
        bfs.pop();
        int zero;
        for(int i = 0;i < 8;i++) {
            if(temp[i] == 0)
                zero = i;
        }
        if(result[conv_n(temp)] <= depth) continue;
        result[conv_n(temp)] = depth;
        if(zero % 4 != 0) {
            swap(temp[zero],temp[zero-1]);
            bfs.push(make_pair(temp,depth+1));
            swap(temp[zero],temp[zero-1]);
        }
        if(zero % 4 != 3) {
            swap(temp[zero],temp[zero+1]);
            bfs.push(make_pair(temp,depth+1));
            swap(temp[zero],temp[zero+1]);
        }
        if(zero < 4) {
            swap(temp[zero],temp[zero+4]);
            bfs.push(make_pair(temp,depth+1));
            swap(temp[zero],temp[zero+4]);
        }
        if(zero >= 4) {
            swap(temp[zero],temp[zero-4]);
            bfs.push(make_pair(temp,depth+1));
            swap(temp[zero],temp[zero-4]);
        }
    }
}

int main(void) {
    solve();
    while(1) {
        if(!(cin >> a[0]))
            break;
        for(int i = 1;i < 8;i++)
            cin >> a[i];
        vector<int> temp;
        for(int i = 0;i < 8;i++) {
            temp.push_back(a[i]);
        }
        cout << (int)result[conv_n(temp)] << endl;
    }
}
