#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>

using namespace std;

typedef long long ll;

map<string,int> mp;

typedef pair<int,int> P;

void bfs() {
    queue<string> que;
    que.push("01234567");
    mp["01234567"] = 0;
    int ans = 1;
    while(!que.empty()) {
        for(int size = que.size(); size; size--) {
            string cur = que.front(); que.pop();
            int pos = cur.find('0');
            //cout << cur << "\n";
            if(pos < 4) {
                string next = cur;
                swap(next[pos], next[pos + 4]);
                if(!mp.count(next)) {
                    mp[next] = ans;
                    que.push(next);
                }
            }
            else {
                string next = cur;
                swap(next[pos], next[pos - 4]);
                if(!mp.count(next)) {
                    mp[next] = ans;
                    que.push(next);
                }
            }
            if(pos != 0 && pos != 4) {
                string next = cur;
                swap(next[pos], next[pos - 1]);
                if(!mp.count(next)) {
                    mp[next] = ans;
                    que.push(next);
                }
            }
            if(pos != 3 && pos != 7) {
                string next = cur;
                swap(next[pos], next[pos + 1]);
                if(!mp.count(next)) {
                    mp[next] = ans;
                    que.push(next);
                }
            }
        }
        ans++;
    }
}

int main() {
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    bfs();
    string line;
    while (getline(cin, line)) {
        line.erase(remove(line.begin(), line.end(), ' '), line.end());
        cout << mp[line] << "\n";
    }
    return 0;
}
