#include <bits/stdc++.h>

using namespace std;

map<string,int> res;   //first -> string, second -> step
string x = "01234567";
struct node{

    string x;
    int step,id;    //id -> 0 index
    node(string x="",int step=0,int id=0):x(x),step(step),id(id){}
};

inline void work(const node &tmp,queue<node> &que,int ct){

    x = tmp.x;
    swap(x[tmp.id],x[tmp.id+ct]);
    if(!res.count(x)){
        que.push(node(x,tmp.step+1,tmp.id+ct));
        res[x] = tmp.step+1;
    }
}
void ini(){

    res[x] = 0;
    queue<node> que;
    que.push(node(x,0,0));

    node tmp;
    while(!que.empty()){
        tmp = que.front(); que.pop();
        if(tmp.step == 40320) return;   //8! = 40320
        if(tmp.id != 3 and tmp.id < 7) work(tmp,que,1);
        if(tmp.id != 4 and tmp.id > 0) work(tmp,que,-1);
        if(tmp.id < 4) work(tmp,que,4);
        if(tmp.id > 3) work(tmp,que,-4);
    }
}
int main(){

    ini();
    string tmp;
    int x;
    while(cin >> x){
        tmp.clear();
        tmp += char(x + '0');
        for(int i = 0; i < 7; i++){
            cin >> x;
            tmp += char(x + '0');
        }
        cout << res[tmp] << endl;
    }

//    system("pause");
    return 0;
}




