#include <vector>
#include <iostream>
#include <climits>
#include <queue>
#include <set>
#include <chrono>
int coord(int state, int d);
static const std::vector<int> order{ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
int swap(const int &state, const int &zero, int target) {
	target = coord(state, target);
	return state - target / 10 + (target % 10) * order.at(zero);
}
int zero_digit(const int &n, const int &z = 0) {
	if (n % 10 == 0) {
		return z;
	}
	else {
		return zero_digit(n / 10, z + 1);
	}
}
int coord(int state, int d) {
	int res = 10;
	for (; d > 0; --d) {
		res *= 10;
		state /= 10;
	}
	return (res + 1) * (state % 10);
}
bool is_not_passed(const int &state, const std::set<int> &memo) {
	return memo.find(state) == memo.end();
}
void renew_memo(const int &state, std::set<int> &memo) {
	memo.insert(state);
}
int wfs(int state) {
	int i = 0;
	std::vector<std::queue<int>> queue(2, std::queue<int>());
	queue.at(i).push(state);
	std::set<int> memo;
	renew_memo(state, memo);
	while (is_not_passed(1234567, memo)) {
		for (; queue.at(i % 2).size() != 0; queue.at(i % 2).pop()) {
			state = queue.at(i % 2).front();
			auto zero = zero_digit(state);
			auto left = swap(state, zero, zero + 1);
			auto right = swap(state, zero, zero - 1);
			auto up = swap(state, zero, (zero + 4) % 8);
			if ((zero % 4 != 3) && is_not_passed(left, memo)) {
				renew_memo(left, memo);
				queue.at((i + 1) % 2).push(left);
			}
			if ((zero % 4 != 0) && is_not_passed(right, memo)) {
				renew_memo(right, memo);
				queue.at((i + 1) % 2).push(right);
			}
			if (is_not_passed(up, memo)) {
				renew_memo(up, memo);
				queue.at((i + 1) % 2).push(up);
			}
		}
		++i;
	}
	return i;
}
int main() {
	int state = 0;
	while (std::cin >> state) {
		int a;
		for (auto i = 0; i < 7; ++i) {
			std::cin >> a;
			state = state * 10 + a;
		}
		//auto start = std::chrono::system_clock::now();
		std::cout << wfs(state) << '\n';
		//auto end = std::chrono::system_clock::now();
		//std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
	}
	return 0;
}