#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

int encode(vector<int> v) {
    int ans = 0;
    for (int j = 0; j < 8; ++j) {
        ans = 8 * ans + v[j];
    }
    return ans;
}

vector<int> decode(int b) {
    vector<int> ans;
    for (int j = 7; j >= 0; --j) {
        ans.push_back((b >> (3*j)) & 7);
    }
    return ans;
}

struct state {
    int pz;
    int count;
    state() {}
    state(int a, int b): pz(a), count(b) {}
};

int puzzle[8];

int visited[1 << 24];

int solve() {
    fill(visited, visited + (1<<24), 0);

    int goal = encode(vector<int>({0, 1, 2, 3, 4, 5, 6, 7}));

    vector<int> stv;
    for (int j = 0; j < 8; ++j) stv.push_back(puzzle[j]);
    int stc = encode(stv);


    queue<state> que;
    que.emplace(stc, 0);
    visited[stc] = 1;
    while (!que.empty()) {
        state st = que.front(); que.pop();

        if (goal == st.pz) {
            return st.count;
        }

        vector<int> pv = decode(st.pz);

        int zi = -1;
        for (int j = 0; j < 8; ++j) {
            if (pv[j] == 0) {
                zi = j;
                break;
            }
        }

        if (zi >= 4) {
            swap(pv[zi], pv[zi-4]);
            int v = encode(pv);
            if (!visited[v]) {
                visited[v] = 1;
                que.emplace(v, st.count+1);
            }
            swap(pv[zi], pv[zi-4]);
        }

        if (zi < 4) {
            swap(pv[zi], pv[zi+4]);
            int v = encode(pv);
            if (!visited[v]) {
                visited[v] = 1;
                que.emplace(v, st.count+1);
            }
            swap(pv[zi], pv[zi+4]);
        }

        if (zi % 4 != 0) {
            swap(pv[zi], pv[zi-1]);
            int v = encode(pv);
            if (!visited[v]) {
                visited[v] = 1;
                que.emplace(v, st.count+1);
            }
            swap(pv[zi], pv[zi-1]);
        }

        if (zi % 4 != 3) {
            swap(pv[zi], pv[zi+1]);
            int v = encode(pv);
            if (!visited[v]) {
                visited[v] = 1;
                que.emplace(v, st.count+1);
            }
            swap(pv[zi], pv[zi+1]);
        }
    }

    return -1;
}

int main() {
    while (cin >> puzzle[0]) {
        for (int j = 1; j < 8; ++j) cin >> puzzle[j];
        cout << solve() << endl;
    }
    return 0;
}