#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

struct P {
    int cnt;
    vi c;
};

const int dxdy[] = {1,4,-1,-4};

bool G[8][8][8][8][8][8][8][8];

int main() {
    vi c(8);
    while (cin >> c[0] >> c[1] >> c[2] >> c[3] >> c[4] >> c[5] >> c[6] >> c[7]) {
        fill_n((bool *)G, 8*8*8*8*8*8*8*8, false);
        queue<P> que;
        que.push({0, c});
        G[c[0]][c[1]][c[2]][c[3]][c[4]][c[5]][c[6]][c[7]] = true;
        while (!que.empty()) {
            P p = que.front(); que.pop();
            if (p.c[0] == 0 && p.c[1] == 1 && p.c[2] == 2 && p.c[3] == 3 &&
                p.c[4] == 4 && p.c[5] == 5 && p.c[6] == 6 && p.c[7] == 7) {
                cout << p.cnt << endl;
                break;
            }
            REP(i, 8) {
                if (p.c[i] != 0) continue;
                REP(j, 4) {
                    int xy = i + dxdy[j];
                    if (!(i == 3 && xy == 4) && !(i == 4 && xy == 3) && 0 <= xy && xy < 8) {
                        swap(p.c[i], p.c[xy]);
                        if (!G[p.c[0]][p.c[1]][p.c[2]][p.c[3]][p.c[4]][p.c[5]][p.c[6]][p.c[7]]) {
                            que.push({p.cnt+1, p.c});
                            G[p.c[0]][p.c[1]][p.c[2]][p.c[3]][p.c[4]][p.c[5]][p.c[6]][p.c[7]] = true;
                        }
                        swap(p.c[i], p.c[xy]);
                    }
                }
            }
        }
    }
}