//C++14 (Clang 3.8.0)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#define rp(i,a,b) for(int (i)=(int)(a);i<(int)(b);++i)

using namespace std;

typedef long long ll;
typedef pair<int,int> P;



int dx[]={1,-1,4,-4};
string ini="01234567";

map<string,int> maps;
set <string> sets;
int bfs(){
    queue<string> que;
    que.push(ini);
    maps[ini]=0;
    sets.insert(ini);
    while(que.size()){
        string p=que.front();que.pop();
        int zero;
        rp(j,0,8) {if(p[j]=='0') {zero=j;break;}}
        rp(j,0,4){
            int pl=zero+dx[j];
            string ns(p);
            bool flag=true;
            if(zero==3 and j==0) flag=false;
            if(zero==4 and j==1) flag=false;
            if(0<=pl and pl<=7 and flag) {
                ns[zero]=p[pl];
                ns[pl]='0';
            
                if(sets.find(ns)==sets.end()) {
                    que.push(ns);
                    sets.insert(ns);
                    maps[ns]=maps[p]+1;
                }
                
            }
        }
    }
}



int main(){
    bfs();
    string s;
    stringstream ss;
    string c;
    while(getline(cin,s)){
        if (s.size()==0) break;
        string c="01234567";
        rp(i,0,8) c[i]=s[i*2];
        cout<<maps[c]<<endl;

    }
        return 0;
};