#include <cstdio>
#include <limits>
#include <cassert>
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <queue>
#include <utility>

using namespace std;

struct S {
    string F;
    int z;
    int t;
    S(const string& F, int z, int t) : F(F), z(z), t(t) {}
};

map<string, int> ANSWER;
void init() {
    string init = "01234567";
    ANSWER[init] = 0;
    queue<S> Q;
    Q.push(S(init, 0, 0));
    vector<int> d[8];
    for (int i = 0; i < 8; i++) {
        if (i % 4 != 0) d[i].push_back(-1);
        if (i % 4 != 3) d[i].push_back(1);
        if (i / 4 == 0) d[i].push_back(4);
        else d[i].push_back(-4);
    }
    while (!Q.empty()) {
        S s = Q.front(); Q.pop();
        int z = s.z;
        int t = s.t;
        for (int i = 0; i < d[z].size(); i++) {
            int nz = z + d[z][i];
            if (nz < 0 || nz >= 8) continue;
            string nF = s.F;
            swap(nF[z], nF[nz]);
            if (ANSWER.count(nF)) continue;
            ANSWER[nF] = t + 1;
            Q.push(S(nF, nz, t + 1));
        }
    }
}

string F;
bool input() {
    F = "";
    for (int i = 0; i < 8; i++) {
        int x; cin >> x;
        if (cin.fail()) return false;
        F.push_back(x + '0');
    }
    //cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return true;
}

void solve() {
    cout << ANSWER[F] << endl;
}

int main() {
    init();
    while (input()) {
        solve();
    }
    return 0;
}