#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <queue>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <vector>
#define LL long long
#define PII pair<int, int>
using namespace std;
const int MAXN = 40;

int sp[(1 << 24) + 5], pa[(1 << 24) + 5];

int encode(const int *arr) {
    int code = 0;
    for (int i = 0; i < 8; i++) {
        code <<= 3;
        code |= arr[i];
    }
    return code;
}

void decode(int *arr, int code) {
    for (int i = 7; i >= 0; i--) {
        arr[i] = code & 7;
        code >>= 3;
    }
}

queue<int> q;

void extend(const int *p, int step, int pre) {
    int code = encode(p);
    if (sp[code] != -1) return;
    sp[code] = step;
    pa[code] = pre;
    q.push(code);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    int p[8] = {0, 1, 2, 3, 4, 5, 6, 7}, init = encode(p);
    memset(sp, -1, sizeof(sp));
    sp[init] = 0;
    q.push(init);
    while (!q.empty()) {
        int code = q.front(), step = sp[code] + 1;
        q.pop();
        decode(p, code);
        for (int i = 0; i < 8; i++) {
            if (p[i] != 0) continue;
            if (i % 4 != 3) {
                swap(p[i], p[i + 1]);
                extend(p, step, code);
                swap(p[i], p[i + 1]);
            }
            if (i % 4 != 0) {
                swap(p[i], p[i - 1]);
                extend(p, step, code);
                swap(p[i], p[i - 1]);
            }
            if (i < 4) {
                swap(p[i], p[i + 4]);
                extend(p, step, code);
                swap(p[i], p[i + 4]);
            }
            if (i >= 4) {
                swap(p[i], p[i - 4]);
                extend(p, step, code);
                swap(p[i], p[i - 4]);
            }
        }
    }

    while (cin >> p[0]) {
        for (int i = 1; i < 8; i++) cin >> p[i];
        cout << sp[encode(p)] << "\n";
    }
}

