#include<iostream>
#include<queue>
int tail[8];
int can[40320][8];
int num[40320];
int dy[4] = {-4,-1,1,4};
struct rem {
	int now;
	int old;
	int dd;
	int qwe;
};
int cnt = 0;

void solve() {
	for (int i = 0; i < 40320; i++) {
		for (int j = 0; j < 8; j++) {
			if (can[i][j] != tail[j]) {
				break;
			}
			if (j == 7) {
				std::cout << num[i] << "\n";
				return;
			}
		}
	}
}

void mokei() {
	std::queue<rem> p;
	rem i = { 0,0,0,0 };
	p.push(i);
	while (p.size()) {
		rem a;
		a = p.front();
		p.pop();
		for (int i = 0; i < 4; i++) {
			if (0 < a.now+dy[i]&&dy[i]!=(-a.old)) {
				cnt++;
				if (cnt < 40320) {
					for (int j = 0; j < 8; j++) {
						can[cnt][j] = can[a.qwe][j];
					}
					int b;
					b = can[cnt][a.now+dy[i]];
					can[cnt][a.now + dy[i]] = can[cnt][a.now];
					can[cnt][a.now] = b;
					 num[cnt] = num[a.qwe] + 1;
					rem q = {a.now+dy[i],dy[i],num[cnt],cnt};
					p.push(q);
				}
				else {
					return;
				}
			}
		}
	}
	return;
}
int main() {
	for (int i = 0; i < 8; i++) {
		can[0][i] = i;
	}
	mokei();
	while (true) {

		for (int i = 0; i < 8; i++) {
			std::cin >> tail[i];
		}
		solve();
	}
	return 0;
}