#include <stdio.h>
#include <queue>
#include <set>
using namespace std;

typedef struct{
	int bd[8];
} BD;

BD bd0;
queue<BD> q;
set<int> visited;
int tms[100000000] = { 0 };

void swap(int& a, int& b){
	int tmp = b;
	b = a;
	a = tmp;
}
int cal_index(BD b){
	int s = 0;
	for(int i = 0; i < 8; i++){
		s += (b.bd[i] << (3*i));
	}
	return s;
}
bool is_ok(BD b){
	for(int i = 0; i < 8; i++){
		if(b.bd[i] != i){
			return false;
		}
	}
	return true;
}

int find0(BD b){
	for(int i = 0; i < 8; i++){
		if(b.bd[i] == 0){
			return i;
		}
	}
	return -1;
}

bool exist(BD b){
	int v = cal_index(b);
	return visited.find(v) != visited.end();
}

int main(){
		for(int i = 0; i < 8; i++){
			bd0.bd[i] = i;
		}
		int times = 0;
		visited = set<int>();
		visited.insert(cal_index(bd0));
		q = queue<BD>();
		q.push(bd0);
		//make tms table
		int loopcont = true;
		while(loopcont){
			loopcont = false;
			int qsize = q.size();
			while(qsize != 0){
				//pop from que
				BD r = q.front(); q.pop();
				tms[cal_index(r)] = times;
				//make move
				int zero = find0(r);
				BD btmp;
				if(zero < 4){
					btmp = r;
					swap(btmp.bd[zero],btmp.bd[zero+4]);
					if(!exist(btmp)){
						q.push(btmp);
						visited.insert(cal_index(btmp));
						loopcont = true;
					}
				}
				else{
					btmp = r;
					swap(btmp.bd[zero],btmp.bd[zero-4]);
					if(!exist(btmp)){
						q.push(btmp);
						visited.insert(cal_index(btmp));
						loopcont = true;
					}
				}
				if(zero != 0 && zero != 4){
					btmp = r;
					swap(btmp.bd[zero],btmp.bd[zero - 1]);
					if(!exist(btmp)){
						q.push(btmp);
						visited.insert(cal_index(btmp));
						loopcont = true;
					}
				}
				if(zero != 3 && zero != 7){
					btmp = r;
					swap(btmp.bd[zero],btmp.bd[zero + 1]);
					if(!exist(btmp)){
						q.push(btmp);
						visited.insert(cal_index(btmp));
						loopcont = true;
					}
				}

				qsize--;
			}
			times++;
		}

	while(true){
		if(scanf("%d%d%d%d%d%d%d%d",&bd0.bd[0],&bd0.bd[1],&bd0.bd[2],&bd0.bd[3],&bd0.bd[4],&bd0.bd[5],&bd0.bd[6],&bd0.bd[7]) == EOF){
			break;
		}
		printf("%d\n",tms[cal_index(bd0)]);
	}
		//init
	return 0;
}

