#include<iostream>
#include<set>
#include<deque>
#include<utility>
#include<algorithm>
#include<stack>
#include<vector>
#include<string.h>

using namespace std;

bool used[8][8][8][8][8][8][8][8];

inline bool judge(vector<int> ar){

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

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

int main(){

    int d[] = {-4,-1,1,4};
    vector< vector<int> > d1;
    for(int i=0;i<8;i++){
	d1.push_back(vector<int>());
	for(int j=0;j<4;j++){
	    if(i+d[j]>-1&&i+d[j]<8){
		if(i == 3 && j==2) continue;
		else if(i == 4 && j == 1) continue;
		d1[i].push_back(i+d[j]);
	    }
	}
    }
		
    vector<int> ar(8);
    deque< vector<int> > next;
    deque< vector<int> > rec;
    int zero,nextzero;
    bool endflag=false,flag;
    int res; 
    while(true){
	memset(used,0,sizeof(used));
	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();
		used[ar[0]][ar[1]][ar[2]][ar[3]][ar[4]][ar[5]][ar[6]][ar[7]] = true;
		zero = findzero(ar);
		next.pop_front();
		for(int i=0;i<d1[zero].size();i++){
		    nextzero = d1[zero][i];
		       swap(ar[nextzero],ar[zero]);
		       if(judge(ar)){
			   flag = true;
			   cout << res << endl;
			   break;
		       }
			if(!used[ar[0]][ar[1]][ar[2]][ar[3]][ar[4]][ar[5]][ar[6]][ar[7]]){
			    rec.push_back(ar);
			}
		       swap(ar[nextzero],ar[zero]);
		}
		if(flag) break;
	    }
	    swap(ne