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

typedef pair<string, int> state;

string swap(string s, int count) {
    char a, b;
    int zero_p = s.find('0');
    a = s[zero_p];
    b = s[zero_p + count];
    s[zero_p] = b;
    s[zero_p + count] = a;
//    cout << s << ' ';
//    cout << zero_p << endl;
    return s;
}

int dx[] = {1, -1, 4, -4};
int main() {
    string str;
    while (getline(cin, str)) {
        str.erase(remove(str.begin(), str.end(), ' '), str.end());
        state init_state(str, 0);
        queue<state> q; q.push(init_state);
        map<string, bool> used; 
        used[init_state.first];
        while (! q.empty() ) {
//            cout << q.size() << endl;
            state s = q.front(); q.pop();
//            cout << s.first << endl;
            if (s.first == "01234567") {
                cout << s.second << endl;
                break;
            }
            if (used[s.first]) continue;
            used[s.first] = true;
            int zero_p = s.first.find('0');
            for (int i = 0; i < 4; i++) {
                if (zero_p  < 4 && i == 3) continue;
                if (zero_p >= 4 && i == 2) continue;
                if (zero_p % 4 == 0 && i == 1) continue;
                if (zero_p % 4 == 3 && i == 0) continue;
//                cout << s.first << ' ' << zero_p << endl;
                state new_state(swap(s.first, dx[i]), s.second+1);
                q.push(new_state);
//                cout << new_state.first << endl;
            }
        }
    }
    return 0;
}