#include<iostream>
#include<queue>
using namespace std;
int G[8][8][8][8][8][8][8][8];
int d[4] = {1, -1, 4, -4};
struct node
{
    int a[8];
    int steps = 0;
    node(){for(int i = 0; i < 8; i++) a[i] = i;}
    bool friend operator==(node x, node y)
    {
        for(int i = 0; i < 8; i++) if(x.a[i] != y.a[i]) return false;
        return true;
    }
};
int main()
{
    node perf;
    queue<node> que;
    que.push(perf);
    while(!que.empty())
    {
        node e = que.front(); que.pop();
        int* a = e.a;
        int zero = 0;
        for(int i = 0; i < 8; i++) if(e.a[i] == 0) {zero = i;}
        //cout<<e.steps << endl;
        for(int i = 0; i < 4; i++)
        {
            if(i == 0 && (zero % 4 == 3)) continue;
            if(i == 1 && (zero % 4 == 0)) continue;
            if(i == 2 && (zero + d[i] > 7)) continue;
            if(i == 3 && (zero + d[i] < 0)) continue;
            node rem_e = e;
            /*for(int i = 0; i < 8; i++) cout<<rem_e.a[i]<<" ";
            cout<<endl;*/
            int tmp = e.a[zero]; e.a[zero] = e.a[zero + d[i]]; e.a[zero + d[i]] = tmp;
            if(e == perf || G[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][a[7]] != 0) {e = rem_e;continue;}
            e.steps++;
            que.push(e);
            G[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][a[7]] = e.steps;
            e = rem_e;/*
            for(int i = 0; i < 8; i++) cout<<e.a[i]<<" ";
            cout<<endl;*/
        }
    }
    int a[10];
    for(int i = 0; i < 10; i++) a[i] = i;
    while(cin>>a[1])
    {
        for(int i = 2; i <= 8; i++) cin >> a[i];
        cout << G[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][a[7]][a[8]] << endl;
    }
    return 0;
}

