#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

bool done[8][8][8][8][8][8][8][8];

int main() {
	cin.tie(false);
	ios::sync_with_stdio(false);

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

	vector<int> goal;
	for(int i = 0; i < 8; ++i)
		goal.push_back(i);

	for(;;) {
		vector<int> first(8);
		for(int i = 0; i < 8; ++i)
			if(!(cin >> first[i]))
				return EXIT_SUCCESS;

		queue<pair<int, vector<int> > > que;
		que.push(make_pair(0, first));
		memset(done, false, sizeof(done));

		int ans = -1;

		while(!que.empty()) {
			int cnt = que.front().first;
			vector<int> s = que.front().second;
			que.pop();

			if(s == goal) {
				ans = cnt;
				break;
			}


			if(done[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]][s[6]][s[7]])
				continue;

			done[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]][s[6]][s[7]] = true;
			int zero;
			for(int i = 0; i < 8; ++i)
				if(s[i] == 0)
					zero = i;

			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;

				vector<int> next = s;
				swap(next[zero], next[ny * 4 + nx]);
				que.push(make_pair(cnt + 1, next));
			}
		}

		cout << ans << endl;
	}
}