#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <algorithm>
#include <utility>

using namespace std;

typedef tuple<string, int, int> TUP;

const string GOAL = "01234567";


int solve(string str, int zeropos) {
	if (str == GOAL) {
		return 0;
	}

	string s;

	set<int> check;

	queue<TUP> que;
	que.push(make_tuple(str, zeropos, 0));

	while (!que.empty()) {
		auto tup = que.front();
		que.pop();

		int pos = get<1>(tup);
		int cnt = get<2>(tup);

		for (int i = 0; i < 3; i++) {
			int to = -1;
			s = get<0>(tup);
			switch (i) {
			case 0:
				to = (pos + 4) % 8;
				break;
			case 1:
				if (pos != 3 && pos != 7) {
					to = (pos + 1) % 4 + pos / 4 * 4;
				}
				break;
			case 2:
				if (pos != 0 && pos != 4) {
					to = (pos + 3) % 4 + pos / 4 * 4;
				}
				break;
			}

			if (to == -1) {
				continue;
			}

			swap(s[pos], s[to]);
			if (s == GOAL) {
				return cnt + 1;
			}

			int num = stoi(s, nullptr, 10);
			if (check.count(num) == 0) {
				que.push(make_tuple(s, to, cnt + 1));
				check.insert(num);
			}
		}
	}
	return -1;
}


int main() {
	int pos;
	string tmp, str(8, '?');

	while (getline(cin, tmp)) {
		for (int i = 0; i < tmp.size(); i += 2) {
			str[i / 2] = tmp[i];
			if (tmp[i] == '0') {
				pos = i / 2;
			}
		}
		cout << solve(str, pos) << endl;
	}

}