#include <iostream>
#include <set>
#include <queue>
#include <algorithm>

using namespace std;

struct p{
	int num[8];
	int pos;	//0?????????
	int t;
};

bool operator <(const p &a,const p &b){
	for (int i=0;i<8;i++){
		if (a.num[i]<b.num[i]){
			return true;
		}
		else if (a.num[i]>b.num[i]){
			return false;
		}
	}
	return false;
}

bool operator >(const p &a,const p &b){
	for (int i=0;i<8;i++){
		if (a.num[i]>b.num[i]){
			return true;
		}
		else if (a.num[i]<b.num[i]){
			return false;
		}
	}
	return false;
}

bool operator ==(const p &a,const p &b){
	for (int i=0;i<8;i++){
		if (a.num[i]!=b.num[i]){
			return false;
		}
	}
	return true;
}

p puzzle;	//?????¨??????????????????
set<p> history;	//?±\??´

p comp;	//????????¢

int res[1000];	//??????

int solve(){
	//???????????????????§£????????????????????°?????????
	history.clear();
	queue<p> que;
	que.push(puzzle);
	history.insert(puzzle);
	while (!que.empty()){
		p puz=que.front();
		que.pop();
		//????????????????????????????????§??????
		if (puz==comp){
			//??????
			return puz.t;
		}
		//?§????
		//???
		p temp;
		if (puz.pos>=4){
			temp=puz;
			swap(temp.num[temp.pos],temp.num[temp.pos-4]);
			temp.pos-=4;
			temp.t++;
			if (history.find(temp)==history.end()){
				//????????¨?????????????±???¢?????´???
				que.push(temp);
				history.insert(temp);
			}
		}
		//???
		if (puz.pos<=3){
			temp=puz;
			swap(temp.num[temp.pos],temp.num[temp.pos+4]);
			temp.pos+=4;
			temp.t++;
			if (history.find(temp)==history.end()){
				que.push(temp);
				history.insert(temp);
			}
		}
		//???
		if (puz.pos!=3 && puz.pos!=7){
			temp=puz;
			swap(temp.num[temp.pos],temp.num[temp.pos+1]);
			temp.pos+=1;
			temp.t++;
			if (history.find(temp)==history.end()){
				que.push(temp);
				history.insert(temp);
			}
		}
		//???
		if (puz.pos!=0 && puz.pos!=4){
			temp=puz;
			swap(temp.num[temp.pos],temp.num[temp.pos-1]);
			temp.pos-=1;
			temp.t++;
			if (history.find(temp)==history.end()){
				que.push(temp);
				history.insert(temp);
			}
		}
	}
}

int main(){
	for (int i=0;i<8;i++){
		comp.num[i]=i;
	}
	comp.pos=0;
	comp.t=0;
	int a;
	int k=0;
	while (cin>>a){
		puzzle.num[0]=a;
		for (int i=1;i<8;i++){
			cin>>puzzle.num[i];
		}
		for (int i=0;i<8;i++){
			if (puzzle.num[i]==0){
				puzzle.pos=i;
				break;
			}
		}
		puzzle.t=0;
		res[k++]=solve();
	}
	for (int i=0;i<k;i++){
		cout<<res[i]<<endl;
	}
	return 0;
}