#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
using namespace std;

int d[4] = {1, -1, 4, -4};
map<string, int> memo;

void swap(string &s, int a, int b){
    char temp = s[a];
    s[a] = s[b];
    s[b] = temp;
}

void bfs(){
    queue<pair<string, int>> q;
    q.push(make_pair("01234567", 0));

    pair<string, int> n;
    while(not q.empty()){
        n = q.front(); q.pop();
        rep(i,4){
            int np = n.second + d[i];
            if(n.second == 3 && np == 4) continue;
            if(n.second == 4 && np == 3) continue;
            if(np < 0 || np >= 8) continue;

            string cps = n.first;
            swap(cps, n.second, np);

            if(memo.count(cps) == 0){
                q.push(make_pair(cps, np));
                memo[cps] = memo[n.first] + 1;
            }
        }
    }
}

int main(){
    memo["01234567"] = 0;
    bfs();
    //show(memo.size())
    string input;
    while(cin >> input){
        range(i,1,8){
            string temp;
            cin >> temp;
            input+=temp;
        }

        cout << memo[input] << endl;
    }
}