#include <stdio.h>
#include <iostream>
#include <queue>

using namespace std;
typedef struct joutai {
	int ii;
	int jj;
	int hyou[2][4];
	int phyou[2][4];
	int counter;
} JOUTAI;
int main() {
	int masu[2][4];
	int si, sj;
	int hantei = 0;
	int hantei2 = 0;
	int counter;
	int di[4] = {1, 0, -1, 0};
	int dj[4] = {0, 1, 0, -1};

	while (cin >> masu[0][0] >> masu[0][1] >> masu[0][2] >> masu[0][3] >> masu[1][0] >> masu[1][1] >> masu[1][2] >> masu[1][3]) {
		queue<JOUTAI> que;
		counter = 0;
		hantei = 0;
		hantei2 = 0;
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 4; j++) {
				if (masu[i][j] == 0) {
					hantei = 1;
					si = i;
					sj = j;
					break;
				}
			}
			if (hantei == 1) {
				break;
			}
		}
		JOUTAI kjou;
		kjou.ii = si;
		kjou.jj = sj;
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 4; j++) {
				kjou.hyou[i][j] = masu[i][j];
				kjou.phyou[i][j] = 0;
			}
		}
		kjou.counter = 0;
		que.push(kjou);
		while (1) {
			JOUTAI jou = que.front();
			que.pop();
			if (jou.hyou[0][0] == 0 && jou.hyou[0][1] == 1 && jou.hyou[0][2] == 2 && jou.hyou[0][3] == 3 && jou.hyou[1][0] == 4 
				&& jou.hyou[1][1] == 5 && jou.hyou[1][2] == 6 && jou.hyou[1][3] == 7) {
				counter = jou.counter;				
				break;
			}
			for (int i = 0; i < 4; i++) {
				int ni = jou.ii + di[i];
				int nj = jou.jj + dj[i];
				if (0 <= ni && ni <= 1 && 0 <= nj && nj <= 3) {
					JOUTAI njou = jou;
					njou.hyou[jou.ii][jou.jj] = jou.hyou[ni][nj];
					njou.hyou[ni][nj] = jou.hyou[jou.ii][jou.jj];
					njou.ii = ni;
					njou.jj = nj;
					njou.counter++;
					for (int s = 0; s < 2; s++) {
						for (int t = 0; t < 4; t++) {
							if (jou.phyou[s][t] != njou.hyou[s][t]) {
								hantei2 = 1;
								break;
							}
						}
					}
					for (int p = 0; p < 2; p++) {
						for (int q = 0; q < 4; q++) {
							njou.phyou[p][q] = jou.hyou[p][q];
						}
					}
					if (hantei2 == 1) {
						que.push(njou);
					}
					hantei2 = 0;
				}
			}
		}
		cout << counter << endl;
	}
	return 0;
}