#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

const int M = (1<<24);
const short int INF = 0x3f3f;

int state;
short int d[M];

bool input()
{
    state = 0;
    for (int i = 0; i < 8; i ++) {
        int tmp;
        if (scanf("%d", &tmp) == EOF)
            return false;
        state = (state<<3) + tmp;
    }
    return true;
}

int hash(int a[])
{
    int res = 0;
    for (int i = 0; i < 8; i ++) {
        res = (res<<3) + a[i];
    }
    return res;
}

int finalArray[8] = {0, 1, 2, 3, 4, 5, 6, 7};
const int finalState = hash(finalArray);

int exchange(int num, int m, int n)
{
    int x = (num>>(m*3)) & 7;
    int y = (num>>(n*3)) & 7;
    if (x && y) return num;
    num &= ~(7 << (m*3));
    num &= ~(7 << (n*3));
    num |= (x << (n*3));
    num |= (y << (m*3));
    return num;
}

int BFS()
{
    fill(d, d+M, INF);
    queue <int> q;
    q.push(state);
    d[state] = 0;
    while( q.size() ) {
        int s = q.front();
        q.pop();
        if (s == finalState)
            return d[s];

        int pos[][2] = {{0, 1}, {1, 2}, {2, 3},
                    {4, 5}, {5, 6}, {6, 7},
                    {0, 4}, {1, 5}, {2, 6}, {3, 7}};
        for (int i = 0; i < 10; i ++) {
            int ns = exchange(s, pos[i][0], pos[i][1]);
            if (s != ns && d[ns] == INF) {
                q.push(ns);
                d[ns] = d[s]+1;
                if (ns == finalState)
                    return d[ns];
            }
        }
    }

    return INF;
}

void solve()
{   
    printf("%d\n", BFS());
}   
    
int main(void)
{   
    while (input()) {
        solve();
    }
    return 0;
}   