#include<cstdio>
#include<algorithm>
#include<memory.h>
#include<queue>
using namespace std;
struct node
{
    int a[8];
    int pos, cnt;
};
const int fac[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
const int mov[4] = {1, -1, 4, -4};
int num[8], fpos;
int goal = 0;
bool vis[40320];

//X = a_n * (n-1)! + a_n-1 * (n-2)! + ... + a_1 * (0)!
int cantor(int *a)
{
    int ans = 0;
    for(int i = 0; i < 8; i++) {
        int cnt = 0;
        for(int j = i+1; j < 8; j++) {
            if(a[j] < a[i]) cnt++;
        }
        ans += cnt*fac[7-i];
    }
    return ans;
}

void Swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}

void bfs()
{
    queue<node> que;
    node temp;
    memset(vis, false, sizeof(vis));
    memcpy(temp.a, num, sizeof(num));
    temp.pos = fpos;
    temp.cnt = 0;
    que.push(temp);
    while(!que.empty()) {
        temp = que.front();
        que.pop();
        int c = cantor(temp.a);
        if(c == goal) {
            printf("%d\n", temp.cnt);
            break;
        }
        vis[c] = true;
        for(int i = 0; i < 4; i++) {
            memcpy(num, temp.a, sizeof(temp.a));
            int tempos = temp.pos + mov[i];
            if(tempos < 0 || tempos > 7 || (temp.pos == 3 && tempos == 4) || (temp.pos == 4 && tempos == 3)) continue;
            Swap(num[temp.pos], num[tempos]);
            if(vis[cantor(num)]) continue;
            node nd;
            memcpy(nd.a, num, sizeof(num));
            nd.pos = tempos;
            nd.cnt = temp.cnt + 1;
            que.push(nd);
        }
    }
    //printf("error\n");
}

void solve()
{
    bfs();
}


int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &num[0])) {
        for(int i = 1; i < 8; i++) {
            scanf("%d", &num[i]);
            if(num[i] == 0) fpos = i;
        }
        solve();
    }
    return 0;
}
