#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

int main() {
	const int dx[4] = {0, 0, -1, 1};
	const int dy[4] = {-1, 1, 0, 0};

	map<string, int> memo;
	const string goal = "01234567";
	queue<pair<int, string> > que;
	que.push(make_pair(0, goal));

	while(!que.empty()) {
		int cnt = que.front().first;
		string state = que.front().second;
		que.pop();

		memo[state] = cnt;

		int zero = state.find('0');

		for(int i = 0; i < 4; ++i) {
			int nx = zero % 4 + dx[i];
			int ny = zero / 4 + dy[i];

			if(nx < 0 || ny < 0 || nx >= 4 || ny >= 2)
				continue;

			string next = state;
			swap(next[zero], next[ny * 4 + nx]);

			if(!memo.count(next))
				que.push(make_pair(cnt + 1, next));
		}
	}

	for(string input; getline(cin, input);) {
		for(int pos; (pos = input.find(" ")) != string::npos;)
			input.erase(pos, 1);

		cout << memo[input] << endl;
	}

	return EXIT_SUCCESS;
}