#include <bits/stdc++.h>

using namespace std;

map<string, int>Dp;
queue < string > strQue;

void calculationAll() {
	int indexOfZero, currentPos;
	int direction[] = { 1,4,-1,-4 };
	string next;

	strQue.push("01234567");
	Dp["01234567"] = 0;
	while (!strQue.empty()) {
		string tmp = strQue.front();
		strQue.pop();
		for (int i = 0; i < 8; i++) {
			if (tmp[i] == '0') {
				indexOfZero = i;
				break;
			}
		}
		for (int i = 0; i < 4; i++) {
			currentPos = indexOfZero + direction[i];
			if (currentPos < 0 || currentPos > 7) {
				continue;
			}
			if ((indexOfZero == 3 && currentPos == 4) || (indexOfZero == 4 && currentPos == 3)) {
				continue;
			}

			next = tmp;
			swap(next[currentPos], next[indexOfZero]);
			if (Dp.find(next) == Dp.end()) {
				Dp[next] = Dp[tmp] + 1;
				strQue.push(next);
			}
		}
	}
}


int main() {
	int num;
	string input, str;
	stringstream ss;

	calculationAll();

	while (getline(cin, str)) {
		input = "";
		ss.clear();
		ss << str;
		while (ss >> num) {
			input += num + '0';
		}
		//cout << input << endl;
		cout << Dp[input] << endl;
	}


	return 0;
}
