#include <iostream>
#include <map>
#include <string>
#include <queue>
#include<algorithm>

using namespace std;


map<string, int> dp;

int go[4] = {1, -1, 4, -4};

void pre_processing(){
    string init = "01234567";
    dp[init] = 0;
    queue<string> que;
    que.push(init);

    while (!que.empty()){
        auto now = que.front(); que.pop();

        int zero = 0;
        for (int i = 0; i < 8; ++ i) if (now[i] == '0') { zero = i; break; }
        for (int i = 0; i < 4; ++ i){
            int nxt_zero = zero + go[i];
            if (nxt_zero < 0 || nxt_zero > 7 || (zero == 3 && i == 0) || (zero == 4 && i == 1)) continue;
            string nxt = now;
            swap(nxt[nxt_zero], nxt[zero]);
            if (dp.find(nxt) == dp.end()){
                dp[nxt] = dp[now] + 1;
                que.push(nxt);
            }
        }
    }
}

int main(){
    dp.clear();

    string query;
    pre_processing();
    while (getline(cin, query)){
        query.erase(std::remove(query.begin(), query.end(), ' '), query.end());
        cout << dp[query] << endl;
    }
    return 0;
}
