#include <iostream>
#include <queue>
#include <string>
#include <map>
#include <stdio.h>

using namespace std;

int dd[] = {1, -1, 4, -4};
map<string, int> done;

int dfs(int cards[7]){
    //その時のカードの状態をqueueに突っ込む
    queue<string> que;
    string first;
    for(int i=0; i<8; i++)
        first+=(cards[i] + '0');
    
    if(first == "01234567")
        return 0;
    
    que.push(first);
    while(!que.empty()){
        string q = que.front(); que.pop();
        int x0 = q.find("0");
        for(int i=0; i<4; i++){
            string t = q;
            int nx = x0+dd[i];
            if(nx<0 || 7<nx || (x0==3&&i==0) || (x0==4&&i==1))
                continue;
            t[x0] = t[nx];
            t[nx] = '0';
            if(done.count(t))
                continue;
            done[t] = done[q] + 1;
            if(t == "01234567")
                return done[t];
            que.push(t);
            
        }
    }
    return 0;
}

int main(){
    int cards[8];
    bool flag = true;
    do{
        for(int i=0; i<8; i++){
            if(scanf("%d", &cards[i]) == EOF)
                flag = false;
        }
        cout << dfs(cards) << endl;
    }while(flag);
    
    return 0;
}