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

using namespace std;

char visited[0xffffff];

int is_visited(unsigned s){ return visited[s&0xffffff]; }
void set_visited(unsigned s){ visited[s&0xffffff]=1; }

unsigned exchange(unsigned s, unsigned p1, unsigned p2, unsigned n){

    unsigned x  = ((s>>p1)^(s>>p2))&((1U<<n)-1);
    unsigned ns = s^((x<<p1)|(x<<p2));

    return ns;

}

int main(){

    unsigned int a[8];

    unsigned f_state = 0;
    
    for(int i=0;i<8;++i) f_state |= (i<<(3*i));

    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]<<(3*i));

        for(int i=0;i<8;++i){
            if(a[i]==0){
                istate |= (i<<24);
            }
        }

        queue<unsigned> q;
        q.push(istate);

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

        int flag = 0;

        for(int n=0;;++n){

            queue<unsigned> nq;

            while(!q.empty()){

                unsigned p = q.front(); q.pop();
                unsigned state = (p&0xffffff);
                unsigned pos = (p>>24);

                if(state == f_state){ flag=1; cout << n << endl; break; }

                int bit_pos = 3*pos;
                unsigned next_state;

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

                    next_state = exchange(state,bit_pos,bit_pos-3,3);
                    next_state |= ((pos-1)<<24);

                    if(!is_visited(next_state)){
                        set_visited(next_state);
                        nq.push(next_state);
                    }

                }

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

                    next_state = exchange(state,bit_pos,bit_pos+3,3);
                    next_state |= ((pos+1)<<24);

                    if(!is_visited(next_state)){
                        set_visited(next_state);
                        nq.push(next_state);
                    }

                }

                if(pos<=3){

                    next_state = exchange(state,bit_pos,bit_pos+12,3);
                    next_state |= ((pos+4)<<24);

                    if(!is_visited(next_state)){
                        set_visited(next_state);
                        nq.push(next_state);
                    }

                }
               
                if(pos>=4){

                    next_state = exchange(state,bit_pos,bit_pos-12,3);
                    next_state |= ((pos-4)<<24);

                    if(!is_visited(next_state)){
                        set_visited(next_state);
                        nq.push(next_state);
                    }

                }

            }

            if(flag)break;

            q = nq;

        }

    }

    return 0;

}