#include<iostream>
#include<queue>
int tail[2][4];
int D[40320][2][4];
int num[40320];
int cnt = 0;
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
struct rem {
	int nowx,nowy;
	int oldx,oldy;
	int innum;
};
rem goodcase;
int testti[2][4];
std::queue<rem> q;
void mokei() {
	rem firstver = { 0,0,0 };
	q.push(firstver);
	while (q.size()) {
		rem situ;
		situ = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int ux = situ.nowx + dx[i];
			int uy = situ.nowy + dy[i];
			if (0 <= ux&&ux < 2 && 0 <= uy&&uy < 4) {
				for (int j = 0; j < 2; j++) {
					for (int k = 0; k < 4; k++) {
						testti[j][k] = D[situ.innum][j][k];
					}
				}
				testti[situ.nowx][situ.nowy] = testti[ux][uy];
				testti[ux][uy] = 0;
				for (int tmp = 0; tmp < cnt; tmp++) {
					for (int j = 0; j < 2; j++) {
						for (int k = 0; k < 4; k++) {
							if (testti[ux][uy] != D[tmp][j][k]) {
								goto rabel1;
							}
							if (k == 7) {
								goto rabel2;
							}
						}
					}
				rabel1:;
				}
				cnt++;
				if (cnt == 40320) {
					return;
				}
				for (int j = 0; j < 2; j++) {
					for (int k = 0; k < 4; k++) {
						D[cnt][j][k] = testti[j][k];
					}
				}
				num[cnt] = num[situ.innum] + 1;
				goodcase = { ux,uy,dx[i],dy[i],cnt };
				q.push(goodcase);
			rabel2:;
			}
		}
	}
	return;
}

int main() {
	int dis = 0;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 4; j++) {
			D[0][i][j] = dis;
			dis++;
		}
	}
	mokei();
	int ques[2][4];
	while (true) {
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 4; j++) {
				std::cin >> ques[i][j];
			}
		}
		for (int k = 0; k < 40320; k++) {
			for (int i = 0; i < 2; i++) {
				for (int j = 0; j < 4; j++) {
					if (D[k][i][j] != ques[i][j]) {
						goto rabel3;
						if (i == 1 && j == 3) {
							std::cout << num[k]<<"\n";
							goto rabel4;
						}
					}
				}
			}
		rabel3:;
		}
	rabel4:;
	}
	return 0;
}