#include <cstdio>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
const string target="01234567";
struct puzzle {
    string stat;
    int cnt;
    int zero;
    puzzle(string _s, int _c, int _x) {
        stat=_s;
        cnt=_c;
        zero=_x;
    }
};
int d[4]={-1,1,-4,4};
int n, res, x;
int main() {
    while (scanf("%d",&n)!=EOF) {
        string stats="";
        stats+=(n+'0');
        if (n==0) x=0;
        for (int i=1; i<8; i++) {
            scanf("%d",&n);
            stats+=(n+'0');
            if (n==0) x=i;
        }
        queue<puzzle> que;
        set<string> done;
        que.push(puzzle(stats,0,x));
        while (!que.empty()) {
            puzzle now = que.front(); que.pop();
            if (now.stat==target) {
                res=now.cnt;
                break;
            }
            for (int i=0; i<4; i++) {
                if (i>=0&&i<8) {
                    string temp=now.stat;
                    swap(temp[now.zero],temp[now.zero+d[i]]);
                    if (done.count(temp)==0) {
                        que.push(puzzle(temp,now.cnt+1,now.zero+d[i]));
                        done.insert(temp);
                    }
                }
            }
        }
        printf("%d\n",res);
    }
}