#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#define INF 100000
using namespace std;
int d[8][8][8][8][8][8][8][8];
int dpos[4] = { 1, -1, 4, -4 };

void swap(int *a, int *b) {
	int tmp = *a;
	*a = *b; *b = tmp;
	return;
}

int bfs(const vector<int>& card) {
	queue<vector<int>> q;

	for (int i = 0; i < 8; ++i)
		for (int j = 0; j < 8; ++j)
			for (int k = 0; k < 8; ++k)
				for (int l = 0; l < 8; ++l)
					for (int m = 0; m < 8; ++m)
						for (int n = 0; n < 8; ++n)
							for (int o = 0; o < 8; ++o)
								for (int p = 0; p < 8; ++p)
									d[i][j][k][l][m][n][o][p] = INF;

	d[card[0]][card[1]][card[2]][card[3]][card[4]][card[5]][card[6]][card[7]] = 0;
	q.push(card);

	while (q.size()) {
		vector<int> b = q.front(); q.pop();

		if (b[0] == 0 && b[1] == 1 && b[2] == 2 && b[3] == 3 && b[4] == 4 && b[5] == 5 && b[6] == 6 && b[7] == 7) break;

		int pos0;
		for (int i = 0; i < 8; ++i) if (b[i] == 0) pos0 = i;
		for (int i = 0; i < 4; ++i) {
			int npos = pos0 + dpos[i];

			if (0 <= npos && npos <= 7 && !(pos0 == 3 && npos == 4) && !(pos0 == 4 && npos == 3)) {
				vector<int> tmp = b;
				swap(&tmp[pos0], &tmp[npos]);
				if (d[tmp[0]][tmp[1]][tmp[2]][tmp[3]][tmp[4]][tmp[5]][tmp[6]][tmp[7]] == INF) {
					q.push(tmp);
					d[tmp[0]][tmp[1]][tmp[2]][tmp[3]][tmp[4]][tmp[5]][tmp[6]][tmp[7]] =
						d[b[0]][b[1]][b[2]][b[3]][b[4]][b[5]][b[6]][b[7]] + 1;
				}
			}
		}
	}
	return d[0][1][2][3][4][5][6][7];
}

int main() {
	int min;
	vector<int> card(8);

	while (cin >> card[0]) {
		for (int i = 1; i < 8; ++i) cin >> card[i];
		min = bfs(card);
		cout << min << endl;
	}
	return 0;
}