#include <iostream>
#include <queue>
#include <string>
#include <map>

using namespace std;

int d[] = {1, -1,  4, -4};

bool check(int space)
{
    return (space >= 0 && space < 8);
}


int main()
{
    map<string, int> m;
    queue<string> que;

    que.push("01234567");
    while (!que.empty()) {
        string now = que.front();
        que.pop();

        int space = now.find("0");
        for (int i = 0; i < 4; i++) {
            if (!check(space + d[i]))
                continue;
            if (space % 4 == 0 && i == 1)
                continue;
            if (space % 4 == 3 && i == 0)
                continue;
            string tmp = now;
            swap(tmp[space], tmp[space + d[i]]);
            if (m.count(tmp) == 0) {
                que.push(tmp);
                m[tmp] = m[now] + 1;
            }
        }
    }

    char c;
    while (cin >> c) {
        string panel;

        panel.push_back(c);
        for (int i = 1; i < 8; i++) {
            cin >> c;
            panel.push_back(c);
        }
        cout << m[panel] << endl;
    }

    return 0;
}