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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int ll;
typedef pair<string, int> P;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    map<string, int> mp;
    queue<P> q;
    q.emplace("01234567", 0);
    while (q.size()) {
        string s = q.front().first;
        int cnt = q.front().second;
        q.pop();
        if (mp.count(s)) continue;
        mp[s] = cnt;
        int zero = -1;
        REP(i, 0, s.size()) {
            if (s[i] == '0') zero = i;
        }
        if (zero % 4 != 0) {
            swap(s[zero], s[zero - 1]);
            q.emplace(s, cnt + 1);
            swap(s[zero], s[zero - 1]);
        }
        if (zero % 4 != 3) {
            swap(s[zero], s[zero + 1]);
            q.emplace(s, cnt + 1);
            swap(s[zero], s[zero + 1]);
        }
        if (zero < 4) {
            swap(s[zero], s[zero + 4]);
            q.emplace(s, cnt + 1);
            swap(s[zero], s[zero + 4]);
        }
        if (zero >= 4) {
            swap(s[zero], s[zero - 4]);
            q.emplace(s, cnt + 1);
            swap(s[zero], s[zero - 4]);
        }
    }

    while (true) {
        string s;
        REP(i, 0, 8) {
            char c;
            if (!(cin >> c)) return 0;
            s += c;
        }
        cout << mp[s] << endl;
    }
    return 0;
}

