#include <vector>
#include <iostream>
#include <stack>
#include <climits>
#include <queue>
const std::vector<int> factorial{ 0, 1, 2, 6, 24, 120, 720, 5040 };
int permutation_to_int(int memo, const int &r, const int &c) {
	if (c == 0) {
		return 0;
	}
	else {
		int res = 0;
		for (auto i = 0; i != r % 10; ++i) {
			if ((memo & (1 << i)) == 0) {
				res += factorial.at(c);
			}
		}
		return res + permutation_to_int(memo | (1 << (r % 10)), r / 10, c - 1);
	}
}
int to_int(const int &r, const int &b = 8) {
	return permutation_to_int(0, r, b - 1);
}
int coord(int state, int d);
int swap(const int &state, const int &zero, int target) {
	target = coord(state, target);
	auto t = target % 10;
	for (auto i = 0; i < zero; ++i) {
		t *= 10;
	}
	return state - target / 10 + t;
}
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);
}
int wfs(int state) {
	std::queue<int> queue;
	queue.push(state);
	std::vector<int> memo(720 * 7 * 8, INT_MAX);
	memo.at(to_int(state)) = 0;
	while (queue.front() != 1234567) {
		state = queue.front();
		auto zero = zero_digit(state);
		auto count = memo.at(to_int(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) && memo.at(to_int(left)) > count + 1) {
			memo.at(to_int(left)) = count + 1;
			queue.push(left);
		}
		if ((zero % 4 != 0) && memo.at(to_int(right)) > count + 1) {
			memo.at(to_int(right)) = count + 1;
			queue.push(right);
		}
		if (memo.at(to_int(up)) > count + 1) {
			memo.at(to_int(up)) = count + 1;
			queue.push(up);
		}
		queue.pop();
	}
	return memo.at(to_int(1234567));
}
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;
		}
		std::cout << wfs(state) << '\n';
	}
	return 0;
}