#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

map<string, int> cntmap;
vector<int> card(8, 0);
int dx[4] = {1, -1, 4, -4};

void bfs() {
    queue<string> que;
    string first = "01234567";
    que.push(first);
    cntmap[first] = 0;
    while(!que.empty()) {
        string now = que.front(); que.pop();
        int npos = 0;
        for(int i = 0; i < 8; ++i) if(now.at(i) == '0') npos = i;
        for(int i = 0; i < 4; ++i) {
            int nx = npos+dx[i];
            if(nx >= 0 && nx < 8 && !((npos == 3 || npos == 7) && i == 0) && !((npos == 0 || npos == 4) && i == 1)) {
                string ns = now;
                swap(ns.at(npos), ns.at(nx));
                if(cntmap[ns] == 0 && first != ns) {
                    cntmap[ns] = cntmap[now]+1;
                    que.push(ns);
                }
            }
        }
    }
}

int main() {
    bfs();
    while(cin >> card.at(0)) {
        for(int i = 1; i < 8; ++i) cin >> card.at(i);
        string ins;
        for(int i = 0; i < 8; ++i) ins.push_back(card.at(i) + '0');
        // cout << ins << endl;
        cout << cntmap[ins] << endl;
    }
}
