//
// Created by Tnecesoc on 2017/3/26.
//

#include <stdio.h>
#include <queue>
#include <map>

using namespace std;

typedef struct {
    int s, t;
} Node;

const int ds[] {4, 1, -4, -1};
const int INF = 0x3fffffff;

int es = 0x01234567;

void swap_bit(int *a, int bit_at_a, int *b, int bit_at_b) {
    int b1 = (*a >> (bit_at_a)) & 1;
    int b2 = (*b >> (bit_at_b)) & 1;
    *a &= ~(1 << bit_at_a);
    *b &= ~(1 << bit_at_b);
    *a |= (b2 << (bit_at_a));
    *b |= (b1 << (bit_at_b));
}

int bfs(int fs) {
    Node p, tp;
    map<int, bool> vis;

    queue<Node> q;
    q.push({fs, 0});
    vis.insert(make_pair(fs, true));

    while (q.size()) {
        p = q.front();
        q.pop();

        if (p.s == es) {
            vis.clear();
            while (q.size()) {
                q.pop();
            }
            return p.t;
        }

        for (int i = 0, mask = 0x07; i < 8; ++i) {

            if ((p.s & mask) == 0) {
                for (int j = 0; j < 4; ++j) {
                    int k = i + ds[j];
                    if (k >= 0 && k < 8 && !(i == 3 && k == 4 || i == 4 && k == 3)) {
                        tp.s = p.s;
                        for (int l = 0; l < 4; ++l) {
                            swap_bit(&tp.s, 4 * i + l, &tp.s, 4 * k + l);
                        }
                        tp.t = p.t + 1;
                        if (!vis.count(tp.s)) {
                            q.push(tp);
                            vis.insert(make_pair(tp.s, true));
                        }
                    }
                }

                break;
            }

            mask <<= 4;
        }
    }

    vis.clear();
    while (q.size()) {
        q.pop();
    }

    return INF;
}

int main() {

    int digit[8], fs;

    while (~scanf("%d", &digit[7])) {
        for (int i = 6; i >= 0; --i) {
            scanf("%d", &digit[i]);
        }

        fs = 0;
        for (int i = 0; i < 8; ++i) {
            fs |= digit[i] << (i << 2);
        }

        int ans = bfs(fs);
        printf("%d\n", ans == INF ? -1 : ans);
    }

    return 0;
}