#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>

using namespace std;

typedef pair<unsigned,unsigned> PII;

char visited[8][8][8][8][8][8][8][8];

int is_visited(unsigned s){

    int a[8];
    for(int i=0;i<8;++i){
        a[i] = (s&0xf);
        s>>=4;
    }

    return visited[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][a[7]];

}

void set_visited(unsigned s){

    int a[8];
    for(int i=0;i<8;++i){
        a[i] = (s&0xf);
        s>>=4;
    }

    visited[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]][a[7]] = 1;

}

void print_state(unsigned s){

    int a[8];
    for(int i=0;i<8;++i){
        a[i] = (s&0xf);
        s>>=4;
    }

    cout << "STATE is ";

    for(int i=0;i<8;++i){

        cout << a[i] << " ";

    }

    cout << endl;

}

int main(){

    unsigned int a[8];

    unsigned f_state = (0 | (1<<4) | (2<<8) | (3<<12) | (4<<16) | (5<<20) | (6<<24) | (7<<28));

    while(cin >> a[0]){

        for(int i=1;i<8;++i) cin >> a[i];

        unsigned istate=0;

        for(int i=0;i<8;++i)istate |= (a[i]<<(4*i));

        memset(visited,0,sizeof(visited));

        priority_queue<PII,vector<PII>,greater<PII> > pq;

        pq.push(make_pair(0,istate));
        set_visited(istate);

        while(!pq.empty()){

            PII p = pq.top(); pq.pop();

            unsigned state = p.second;

            if(state == f_state){
                cout << p.first << endl;
                break;
            }

            for(int i=0;i<8;++i){

                if(((state>>(4*i))&0xf)==0){

                    int pos = 4*i;

                    if(i!=0 && i!=4){

                        unsigned x = ((state>>pos)^(state>>(pos-4)))&((1U<<4)-1);
                        unsigned next = state^((x<<pos)|(x<<(pos-4)));

                        if(!is_visited(next)){
                            set_visited(next);
                            pq.push(make_pair(p.first+1,next));
                        }

                    }

                    if(i!=3 && i!=7){

                        unsigned x = ((state>>pos)^(state>>(pos+4)))&((1U<<4)-1);
                        unsigned next = state^((x<<pos)|(x<<(pos+4)));

                        if(!is_visited(next)){
                            set_visited(next);
                            pq.push(make_pair(p.first+1,next));
                        }

                    }

                    if(i<=3){

                        unsigned x = ((state>>pos)^(state>>(pos+16)))&((1U<<4)-1);
                        unsigned next = state^((x<<pos)|(x<<(pos+16)));

                        if(!is_visited(next)){
                            set_visited(next);
                            pq.push(make_pair(p.first+1,next));
                        }

                    }

                    if(i>=4){

                        unsigned x = ((state>>pos)^(state>>(pos-16)))&((1U<<4)-1);
                        unsigned next = state^((x<<pos)|(x<<(pos-16)));

                        if(!is_visited(next)){
                            set_visited(next);
                            pq.push(make_pair(p.first+1,next));
                        }

                    }

                }

            }

        }

    }

    return 0;

}