#include <iostream>
#include <queue>
#include <map>
#include <cmath>
using namespace std;

class Cards{
public:
    int n[8];
    unsigned to_key(){
        unsigned key = 0;
        for(int i=0;i<8;i++)
            key += n[i]*pow(10,7-i);
        return key;
    }
    int get_p(){
        int i;
        for(i=0;i<8;i++){
            if(n[i]==0)
                break;
        }
        return i;
    }
};

int main(){
    while(true){
        Cards cards;
        for(int i=0;i<8;i++)    //入力
            cin >> cards.n[i];
        
        if(cin.eof())           //EOFで終了
            break;

        queue<Cards> que;
        que.push(cards);

        map<unsigned, int> puz;
        puz[cards.to_key()] = 0;

        while(!que.empty()){
            cards = que.front();
            que.pop();

            if(cards.to_key() == 1234567){    //整列完了
                cout << puz[1234567] << endl;
                break;
            }

            int p = cards.get_p();

            if(!(0<=p && p<=3)){    //上と交換
                Cards cards2 = cards;
                swap(cards2.n[p],cards2.n[p-4]);
                if(puz.find(cards2.to_key()) == puz.end()){
                    que.push(cards2);
                    puz[cards2.to_key()] = puz[cards.to_key()]+1;
                }
            }
            if(!(4<=p && p<=7)){    //下と交換
                Cards cards2 = cards;
                swap(cards2.n[p],cards2.n[p+4]);
                if(puz.find(cards2.to_key()) == puz.end()){
                    que.push(cards2);
                    puz[cards2.to_key()] = puz[cards.to_key()]+1;
                }
            }
            if(!(p==0 || p==4)){    //左と交換
                Cards cards2 = cards;
                swap(cards2.n[p],cards2.n[p-1]);
                if(puz.find(cards2.to_key()) == puz.end()){
                    que.push(cards2);
                    puz[cards2.to_key()] = puz[cards.to_key()]+1;
                }
            }
            if(!(p==3 || p==7)){    //右と交換
                Cards cards2 = cards;
                swap(cards2.n[p],cards2.n[p+1]);
                if(puz.find(cards2.to_key()) == puz.end()){
                    que.push(cards2);
                    puz[cards2.to_key()] = puz[cards.to_key()]+1;
                }
            }
        }
    }

    return 0;
}
