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

using namespace std;

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

struct Puzzle {
    string panel;
    int space, cnt;

    Puzzle(string panel_, int space_, int cnt_) {
        panel = panel_;
        space = space_;
        cnt = cnt_;
    }
    Puzzle(){}
};

bool check(string s) 
{
    if (s == "01234567")
        return true;
    else
        return false;
}

int bfs(Puzzle s)
{
    map<string, bool> m;
    queue<Puzzle> que;

    que.push(s);
    m[s.panel] = true;

    while (!que.empty()) {
        Puzzle now = que.front();
        que.pop();

        if (check(now.panel))
            return now.cnt;

        int idx1 = now.space;
        for (int i = 0; i < 4; i++) {
            int idx2 = idx1 + d[i];

            if (idx2 < 0 || idx2 >= 8)
                continue;
            string next_panel = now.panel;

            next_panel[idx1] = now.panel[idx2];
            next_panel[idx2] = now.panel[idx1];

            Puzzle next;
            next.panel = next_panel;
            next.space = idx2;
            next.cnt = now.cnt + 1;

            if (m.count(next.panel) == 0) {
                m[next.panel] = true;
                que.push(next);
            }
        }
    }
    return -1;
}


int main()
{
    int space;
    char c;

    while (cin >> c) {
        string panel;

        if (c == '0')
            space = 0;
        panel.push_back(c);
        for (int i = 1; i < 8; i++) {
            cin >> c;
            panel.push_back(c);
            if (c == '0')
                space = i;
        }
        Puzzle s(panel, space, 0);
        cout << bfs(s) << endl;
    }

    return 0;
}