#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <string>
#include <iostream>

using namespace std;
const int INF = 0x3f3f3f3f;

const int dir[4] = {-1, 1, -4, 4};

unordered_map<string, int> um;
string s;

void bfs() {
    queue<string> que;
    const string ans = "01234567";
    um[ans] = 0; que.push(ans);
    while(!que.empty()) {
        string now = que.front(); que.pop();
        if(now == s) return;
        int pos = 0;
        for(int i = 1; i < 8; ++i) {
            if(now[i] == '0') {
                pos = i; break;
            }
        }
        for(int i = 0; i < 4; ++i) {
            int tpos = pos + dir[i];
            if(pos == 4 && i == 0) continue;
            if(pos == 3 && i == 1) continue;
            if(tpos < 0 || tpos > 7) continue;
            string temp = now;
            swap(temp[pos], temp[tpos]);
            if(um.find(temp) == um.end()) {
                um[temp] = um[now] + 1;
                que.push(temp);
            }
        }
    }
}

int main() {
    bfs();
    while(getline(cin, s)) {
        s.erase(remove(s.begin(), s.end(), ' '), s.end());
        printf("%d\n", um[s]);
    }
    return 0;
}

