#include<iostream>
#include<set>
#include<deque>
#include<utility>
#include<algorithm>
#include<stack>
#include<vector>

using namespace std;

bool judge(vector<int> ar){

    for(int i=0;i<ar.size();i++){
	if (ar[i] != i) return false;
    }
    return true;
}

int findzero(vector<int> ar){
    for(int i=0;i<8;i++){
	if(ar[i] == 0) return i;
    }
    return 0;
}

int main(){

    vector<int> ar(8);
    set< vector<int> > memo1;
    set< vector<int> > memo2;
    deque< vector<int> > next;
    deque< vector<int> > rec;
    int d[] = {-4,-1,1,4};
    int zero;
    bool endflag=false,flag;
    int res; 
    while(true){
	res = 0;
	flag = false;
	for(int i=0;i<8;i++){
	    if((cin >> ar[i])==0){
		endflag = true;
		break;
	    }
	    if(ar[i] == 0) zero = i;
	}
	if(endflag) break;
	next.push_back(ar);
	if(judge(ar)){ 
	    flag = true; 
	    cout << res << endl;
	}
	while(!flag){
	    res++;
	    while(!next.empty()){
		ar = next.front();
		memo2.insert(ar);
		zero = findzero(ar);
		for(int i=0;i<4;i++){
		    if(zero+d[i]>-1&&zero+d[i]<8){
			if(zero == 3 && i==2) continue;
			else if(zero == 4 && i == 1) continue;
		       swap(ar[zero+d[i]],ar[zero]);
		       if(judge(ar)){
			   flag = true;
			   cout << res << endl;
			   break;
		       }
			if(memo1.count(ar) == 0){
			   rec.push_back(ar);
		       }
		       swap(ar[zero+d[i]],ar[zero]);
		    }
		}
		if(flag) break;
		next.pop_front();
	    }
	    memo1.clear();
	    swap(memo1,memo2);
	    swap(next,rec);
	}
	if(flag){
	    next.clear();
	    rec.clear();
	    memo1.clear();
	    memo2.clear();
	}

    }
    return 0;
}