#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<string>

using namespace std;
set<string> st;
map<string,int> mp;
const int xx[] = {1,4,-1,-4};
class Node{
public:
    string str;
    int pos;
    int Time;
    Node(string ss,int p,int t){
        str = ss;
        pos = p;
        Time = t;
    }
    Node(){
        str = "";
        Time = 0;
        pos = 0;
    }
};
bool Check(int x){
    if(x < 0 || x > 7)  return false;
    return true;
}

void bfs(){
    st.clear();
    mp.clear();
    queue<Node> q;
    q.push(Node("01234567",0,0));
    mp["01234567"] = 0;
    st.insert("01234567");
    while(!q.empty()){
        Node basic = q.front();q.pop();
        for(int i=0;i<4;++i){
            if(basic.pos == 3 && i == 1)    continue;
            if(basic.pos == 4 && i == 3)    continue;
            int tx = basic.pos + xx[i];
            int tt = basic.Time + 1;
            if(!Check(tx))  continue;
            string str = basic.str;
            swap(str[tx],str[basic.pos]);
            if(!st.count(str)){
                st.insert(str);
                mp[str] = tt;
                q.push(Node(str,tx,tt));
            }
        }
    }
    
}

int main(void){
    string str;
    bfs();
    while(getline(cin,str)){
        str.erase(remove(str.begin(),str.end(),' '),str.end());
        cout << mp[str] << endl;
    }
    cout << str << endl;
    
    return 0;
}