#include <bits/stdc++.h>


using namespace std;
const int inf = 1e+9;
int L = 8;
int spos = 0;


map<string, int> st;
struct P{
    int pos;
    string status;
    int step;
};
string str = "01234567";
void dp(){
    queue<P> que;
    que.push((P){spos, str, 0});
    st[str] = 0;

    while(!que.empty()){
        P p = que.front();que.pop();
        int pos = p.pos;
        string status = p.status;
        int step = p.step;

        if((pos+4)<L){//可以下移
            string newstatus = status;
            swap(newstatus[pos], newstatus[pos+4]);
            if(st.find(newstatus) == st.end()){
                que.push((P){pos+4, newstatus, step+1});
                st[newstatus] = step+1;
            }
        }

        if((pos-4)>=0){//可以上移
            string newstatus = status;
            swap(newstatus[pos], newstatus[pos-4]);
            if(st.find(newstatus) == st.end()){
                que.push((P){pos-4, newstatus, step+1});
                st[newstatus] = step+1;
            }
        }

        if((pos+1)%4){//可以右移
            string newstatus = status;
            swap(newstatus[pos], newstatus[pos+1]);
            if(st.find(newstatus) == st.end()){
                que.push((P){pos+1, newstatus, step+1});
                st[newstatus] = step+1;
            }
        }

        if(pos%4){//可以左移
            string newstatus = status;
            swap(newstatus[pos], newstatus[pos-1]);
            if(st.find(newstatus) == st.end()){
                que.push((P){pos-1, newstatus, step+1});
                st[newstatus] = step+1;
            }
        }
    }

}

int main(){
    int num;
    dp();
    while(~scanf("%d", &num)){
        str="";
        str.push_back(num+'0');
        if(num == 0) spos = 0;
        for(int i = 1;i < L;i++){
            scanf("%d", &num);
            str.push_back(num+'0');
            if(num == 0) spos = i;
        }
        if(st.find(str) == st.end()) printf("-1\n");
        else printf("%d\n",st[str]);
    }
    return 0;

}

/*
0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0
1 5 2 0 3 7 4 6
 */
