#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]; }
void set_visited(unsigned s){ visited[s]=1; }

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));

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

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

        set_visited(istate);

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

            queue<unsigned> nq;

            while(!q.empty()){

                unsigned state = q.front(); q.pop();

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

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

                    if(((state>>(3*i))&0x7)!=0)continue;

                    int pos = 3*i;

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

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

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

                    }

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

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

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

                    }

                    if(i<=3){

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

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

                    }

                    if(i>=4){

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

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

                    }

                    break;

                }

            }

            if(nq.empty())break;

            q = nq;

        }

    }

    return 0;

}