#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    using P = array<char, 8>;
    map<P, int> m; m[{0, 1, 2, 3, 4, 5, 6, 7}] = 0;
    P x = {0, 1, 2, 3, 4, 5, 6, 7};
    queue<P> Q;
    Q.emplace(x);
    while(!Q.empty()){
        P a = Q.front(); Q.pop();
        int val = m[a];
        for (int i = 0; i < 8; ++i) {
            if(!a[i]){
                swap(a[i], a[i^4]);
                if(!m.count(a)){
                    m[a] = val+1;
                    Q.emplace(a);
                }
                swap(a[i], a[i^4]);
                if(i&3){
                    swap(a[i-1], a[i]);
                    if(!m.count(a)){
                        m[a] = val+1;
                        Q.emplace(a);
                    }
                    swap(a[i-1], a[i]);
                }
                if((i&3) != 3){
                    swap(a[i], a[i+1]);
                    if(!m.count(a)){
                        m[a] = val+1;
                        Q.emplace(a);
                    }
                    swap(a[i], a[i+1]);
                }
            }
        }
    }
    while(scanf("%hhd", &x[0]) != EOF){
        for (int i = 1; i < 8; ++i) scanf("%hhd", &x[i]);
        cout << m[x] << "\n";
    }
    return 0;
}
