#include <bits/stdc++.h>

using namespace std;

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


int main() {
    string raw;
    string ans = "01234567";
    while (getline(cin, raw)) {
        if (raw == "") continue;
        string init;
        for (auto c : raw) {
            if (c == ' ') continue;
            init.push_back(c);
        }
        queue<pair<string, int> > Q;
        unordered_map<string, bool> visited;
        Q.push({init, 0});
        visited[init] = true;
        bool is_exit = false;
        while ((not Q.empty()) and (not is_exit)) {
            string cur;
            int step;
            tie(cur, step) = Q.front(); Q.pop();
//            cout << cur << endl;
            if (cur == ans) {
                cout << step << endl;
                is_exit = true;
            }
            int id = cur.find('0');
            int x = id / 4, y = id % 4;
            for (auto k : d) {
                int nx = k[0] + x;
                int ny = k[1] + y;
                if (nx >= 0 and nx < 2 and ny >= 0 and ny < 4) {
                    int nid = nx * 4 + ny;
                    string s = cur;
                    swap(s[id], s[nid]);
                    if (not visited[s]) {
                        visited[s] = true;
                        Q.push({s, step + 1});
                    }
                }
            }
        }
    }
}
