#include <iostream>
#include <set>
#include <queue>
using namespace std;

class Info{
public:
	vector<int> v;
	int depth;
};

bool is_ans(vector<int> v){
	for(int i=0;i<8;i++){
		if(v[i] != i){return false;}
	}
	return true;
}

vector<int> slide_piece(vector<int> v,int dir){
	int from,to;

	for(from=0;v[from]!=0;from++);

	if(dir == 0){
		to = from - 4;
		if(to >= 0){
			swap(v[from],v[to]);
		}
	}
	else if(dir == 1){
		to = from + 1;
		if(to < 8 && from/4 == to/4){
			swap(v[from],v[to]);
		}
	}
	else if(dir == 2){
		to = from + 4;
		if(to < 8){
			swap(v[from],v[to]);
		}
	}
	else{
		to = from - 1;
		if(to >= 0 && from/4 == to/4){
			swap(v[from],v[to]);
		}
	}

	return v;
}

int bfs(vector<int> v){
	if(is_ans(v)){return 0;}

	set< vector<int> > s;
	queue<Info> q;
	vector<int> next_v;

	Info next_info,now_info = {v,0};
	q.push(now_info);
	s.insert(v);

	while(!q.empty()){
		now_info = q.front(); q.pop();

		for(int i=0;i<4;i++){
			next_v = slide_piece(now_info.v,i);

			if(now_info.v != next_v && s.find(next_v) == s.end()){
				if(is_ans(next_v)){
					return now_info.depth + 1;
				}

				next_info.v = next_v;
				next_info.depth = now_info.depth + 1;
				q.push(next_info);
				s.insert(next_v);
			}
		}
	}
}

int main(void){
	int i;
	vector<int> v(8);

	while(1){
		for(i=0;i<8;i++){
			if(!(cin>>v[i])) return 0;
		}
		cout<<bfs(v)<<endl;
	}
}