#include <iostream>
#include <queue>
#define INF 9999999
using namespace std;
typedef struct {
	int d[9];	
}data;
int main() {
	while(true) {
		queue<data> bfs;
		data first;
		bool checked[8][8][8][8][8][8][8];

		int min = INF;
		for (int i = 0;i < 8;i++) {
			cin >> first.d[i];
		}
		first.d[8] = 0;
		bfs.push(first);
		for (;;) {
			if (bfs.empty()) break;
			data front;
			front = bfs.front();
			bfs.pop();
			for (int k = 0;k < 4;k++) {
				cout << front.d[k] << " ";
			}
			cout << endl;
			for (int k = 4;k < 8;k++) {
				cout << front.d[k] << " ";
			}
			cout << endl;
			//Is it already covered?
			if(checked[front.d[0]][front.d[1]][front.d[2]][front.d[3]][front.d[4]][front.d[5]][front.d[6]][front.d[7]] == true) {
				cout << "skipped" << endl;
				continue;
			}
			else {
				cout << "NOT" << endl;
			}
			//Check
			if (front.d[8] < min) {
				for (int i = 0;i < 8;i++) {
					if(front.d[i] != i) goto outofcheck;
				}
				min = front.d[8];
			}
			outofcheck:
			//Find Zero
			int zero;
			for (int i = 0;i < 8;i++) {
				if (front.d[i] == 0) {
					zero = i;
					break;
				}
			}
			//Move
			if (zero < 4) {
				//???????????\?????????
				int x;
				data array = front;
				array.d[8] = front.d[8] + 1;
				x = array.d[zero];
				array.d[zero] = array.d[zero+4];
				array.d[zero+4] = x;
				bfs.push(array);
				if (zero>0) {
					int y;
					data array2 = front;
					array2.d[8] = front.d[8] + 1;
					y = array2.d[zero];
					array2.d[zero] = array2.d[zero-1];
					array2.d[zero-1] = y;
					bfs.push(array2);
				}
				if(zero<3) {
					int z;
					data array3 = front;
					array3.d[8] = front.d[8] + 1;
					z = array3.d[zero];
					array3.d[zero] = array3.d[zero+1];
					array3.d[zero+1] = z;
					bfs.push(array3);
				}
			}
			else {
				//???????????\?????????
				int x;
				data array = front;
				array.d[8] = front.d[8] + 1;
				x = array.d[zero];
				array.d[zero] = array.d[zero-4];
				array.d[zero-4] = x;
				bfs.push(array);
				if (zero>4) {
					int y;
					data array2= front;
					array2.d[8] = front.d[8] + 1;
					y = array2.d[zero];
					array2.d[zero] = array2.d[zero-1];
					array2.d[zero-1] = y;
					bfs.push(array2);
				}
				if(zero<7) {
					int z;
					data array3 = front;
					array3.d[8] = front.d[8] + 1;
					z = array3.d[zero];
					array3.d[zero] = array3.d[zero+1];
					array3.d[zero+1] = z;
					bfs.push(array3);
				}
			}
			checked[front.d[0]][front.d[1]][front.d[2]][front.d[3]][front.d[4]][front.d[5]][front.d[6]][front.d[7]] = true;
		}
	cout << min << endl;
	}
	return 0;
}