#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <map>

using namespace std;

#define MAX_HIT ((0<<0)|(1<<3)|(2<<6)|(3<<9)|(4<<12)|(5<<15)|(6<<18)|(7<<21))

bool hit[MAX_HIT+1];

#define POS(value, i) ((value>>(i+(i<<1)))&7)

inline int swap_pos(int value, int i, int j)
{
	int vi, vj;
	i*=3; j*=3;
	vi = (value>>i) & 7;
	vj = (value>>j) & 7;

	value = (value & ~(7<<i)) | (vj<<i);
	value = (value & ~(7<<j)) | (vi<<j);
	return value;

}


int main()
{
	//freopen("in.txt", "r", stdin);

	int target, src;
	int i, j;

	target=0;
	for (i=0; i<=7; i++) {
		target = (target<<3)|i;
	}

	//printf("MAX_HIT=0x%x\n", MAX_HIT);

	while (scanf("%d", &src)==1) {
		for (i=1; i<=7; i++) {
			int tmp;
			scanf("%d", &tmp);
			src = (src<<3) | tmp;
		}

		//printf("target=%d, src=%d\n", target, src);

		memset(hit, 0, sizeof(hit));

		hit[src] = true;

		queue<int> q1, q2, *q, *nq, *qtmp;
		q = &q1; nq = &q2;
		q1.push(src);
		int step = 0;

		while (!hit[target]) {
			while (!nq->empty()) nq->pop();
			step++;
			while (!q->empty()) {
				int value = q->front(); q->pop();
				int next_value;

				for (i=0; i<=7; i++) {
					if (POS(value, i)!=0) continue;

					if (i<4) {
						next_value = swap_pos(value, i, i+4);
						if (hit[next_value]==false) {
							hit[next_value] = true;
							nq->push(next_value);
						}
					}
					else {
						next_value = swap_pos(value, i, i-4);
						if (hit[next_value]==false) {
							hit[next_value] = true;
							nq->push(next_value);
						}
					}

					if (i!=0 && i!=4) {
						next_value = swap_pos(value, i, i-1);
						if (hit[next_value]==false) {
							hit[next_value] = true;
							nq->push(next_value);
						}
					}

					if (i!=3 && i!=7) {
						next_value = swap_pos(value, i, i+1);
						if (hit[next_value]==false) {
							hit[next_value] = true;
							nq->push(next_value);
						}
					}
				}
				if (hit[target]) break;
			}

			qtmp = q; q = nq; nq = qtmp;

		}
		printf("%d\n", step);

	}

	return 0;
}