#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;
const int INF = 30;
using namespace std;

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

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

void bfs(string s, int p, int cnt){
    if(s == input){
        ans = min(ans, cnt);
        return;
    }else if(cnt >= ans){
        return;
    }


    rep(i,4){
        int np = p + d[i];
        if(p == 4 && d[i] == 1) continue;
        else if(p == 5 && d[i] == -1) continue;
        if(np < 0 || np >= 8) continue;

        string cps = s;
        swap(cps, p, np);

        if(memo[cps] == 0 || memo[cps] > cnt){
            memo[cps] = cnt;
            bfs(cps, np, cnt + 1);
        }
    }
}

int main(){
    while(cin >> input){
        string s = "01234567";
        int z = 0;
        ans = INF;

        range(i,1,8){
            string temp;
            cin >> temp;
            if(temp == "0") z = i;
            input+=temp;
        }

        bfs(s, z, 0);

        cout << ans << endl;
    }
}