#include <cstdio>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;

map<string, int> m;

void build(void) {
    m["01234567"] = 0;
    queue<string> q;
    q.push("01234567");
    while (!q.empty()) {
        string t = q.front();q.pop();
        int i;
        for (i = 0; i < 8; i++) {
            if (t[i] == '0') break;
        }
        if (i != 0 && i != 4) {
            string u = t;
            swap(u[i], u[i - 1]);
            if (m.count(u) == 0) {
                q.push(u);
                m[u] = m[t] + 1;
            }
        }
        if (i != 3 && i != 7) {
            string u = t;
            swap(u[i], u[i + 1]);
            if (m.count(u) == 0) {
                q.push(u);
                m[u] = m[t] + 1;
            }
        }
        if (i > 3) {
            string u = t;
            swap(u[i], u[i - 4]);
            if (m.count(u) == 0) {
                q.push(u);
                m[u] = m[t] + 1;
            }
        }
        if (i < 4) {
            string u = t;
            swap(u[i], u[i + 4]);
            if (m.count(u) == 0) {
                q.push(u);
                m[u] = m[t] + 1;
            }
        }
    }
}

int main(void) {
    build();
    while (1) {
        string s;
        char c;
        for (int i = 0; i < 8; i++) {
            if(scanf(" %c", &c) == EOF) return 0;
            s += c;
        }
        printf("%d\n", m[s]);
    }
    return 0;
}