#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

signed main(){
    while(1){
        char c;
        string s = "";
        rep(i, 0, 8){
            cin >> c;
            s += c;
        }
        if(cin.eof()) break;
        
        queue<string> q;
        q.push(s);
        set<string> st;
        st.insert(s);
        map<string, int> mp;
        mp[s] = 0;
        int ans = -1;

        while(q.size()){
            string now = q.front(); q.pop();
            if(now == "01234567"){
                ans = mp[now]; break;
            }
            string next = now;
            int ind = -1;
            rep(i, 0, 8){
                if(now[i] == '0') ind = i;
            }
            if(ind % 4 != 0){
                next[ind] = next[ind - 1];
                next[ind - 1] = '0';
            }
            if(!st.count(next)){
                q.push(next);
                mp[next] = mp[now] + 1;
            }
            next = now;
            if(ind % 4 != 3){
                next[ind] = next[ind + 1];
                next[ind + 1] = '0';
            }
            if(!st.count(next)){
                q.push(next);
                mp[next] = mp[now] + 1;
            }
            next = now;
            next[ind] = next[(ind + 4) % 8];
            next[(ind + 4) % 8] = '0';
            if(!st.count(next)){
                q.push(next);
                mp[next] = mp[now] + 1;
            }
        }
        cout << ans << endl;
    }
}