#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <map>

using namespace std;


int dir[4] = {-1, 1, -4, 4};
int input[8];


int main(){
    while(scanf("%d %d %d %d %d %d %d %d", &input[0], &input[1], &input[2], &input[3], &input[4], &input[5], &input[6], &input[7]) != EOF){
        string in, cur;
        for(int i=0; i<8; ++i) in.push_back(input[i]+'0');

        queue<string> myqueue;
        myqueue.push(in);
        map<string, int> table;
        table.insert(make_pair(in,0));

        while(!myqueue.empty()){

            cur = myqueue.front();

            myqueue.pop();

            bool finish = true;
            for(int i=0; i<8; ++i){
                if(cur[i]-'0' != i){
                    finish = false;
                    break;
                }
            }
            if(finish){
                cout << table.find(cur)->second << endl;
                break;
            }

            int zero;
            for(int i=0; i<8; ++i){
                if(cur.at(i)=='0'){
                    zero = i;
                    break;
                }
            }

            for(int i=0; i<4; ++i){
                if(zero + dir[i] < 0 || zero + dir[i] > 7) continue;
                if(zero == 3 && i == 1) continue;
                if(zero == 4 && i == 0) continue;

                string temp = cur;
                swap(temp.at(zero), temp.at(zero+dir[i]));
                if(table.find(temp) == table.end()){
                    table.insert(make_pair(temp, table.find(cur)->second+1));
                    myqueue.push(temp);
                }
            }
        }
    }

    return 0;
}


