#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;

struct data{
	unsigned long value;
	int num;

	data(long v, int n) {
		value = v; num = n;
	}
	data() {}
};

int main() {
	string in;

	while(getline(cin, in)) {
		stringstream sstr(in);
		unsigned long l = 0; int tmp;
		while(sstr >> tmp) {
			l <<= 3;
			l |= tmp;
		}

		queue<data> que;
		que.push(data(l,0));
		set<unsigned long> used;
		used.insert(l);

		while(!que.empty()) {
			data d;
			d = que.front(); que.pop();
			if(d.value == 001234567) {
				cout << d.num << endl;
				break;
			}

			/* decode */
			int board[2][4];
			for(int i = 0; i < 2; i++) {
				for(int j = 0; j < 4; j++) {
					board[i][j] = ((070000000 >> ((i * 4 + j) * 3)) & d.value) >> ((7 - (i * 4 + j)) * 3);
				}
			}
			/* search */
			for(int i = 0; i < 2; i++) {
				for(int j = 0; j < 4; j++) {
					if(!board[i][j]) {
						if(i != 0) {
							swap(board[i - 1][j], board[i][j]);
							unsigned long enc = 0;
							for(int k = 0; k < 8; k++) {
								enc |= board[0][k] << (21 - k * 3);
							}
							if(used.find(enc) == used.end()) {
								used.insert(enc);
								que.push(data(enc, d.num + 1));
							}
							swap(board[i - 1][j], board[i][j]);
						}
						if(i != 1) {
							swap(board[i][j], board[i + 1][j]);
							unsigned long enc = 0;
							for(int k = 0; k < 8; k++) {
								enc |= board[0][k] << (21 - k * 3);
							}
							if(used.find(enc) == used.end()) {
								used.insert(enc);
								que.push(data(enc, d.num + 1));
							}
							swap(board[i][j], board[i + 1][j]);
						}
						if(j != 0) {
							swap(board[i][j - 1], board[i][j]);
							unsigned long enc = 0;
							for(int k = 0; k < 8; k++) {
								enc |= board[0][k] << (21 - k * 3);
							}
							if(used.find(enc) == used.end()) {
								used.insert(enc);
								que.push(data(enc, d.num + 1));
							}
							swap(board[i][j - 1], board[i][j]);
						}
						if(j != 3) {
							swap(board[i][j], board[i][j + 1]);
							unsigned long enc = 0;
							for(int k = 0; k < 8; k++) {
								enc |= board[0][k] << (21 - k * 3);
							}
							if(used.find(enc) == used.end()) {
								used.insert(enc);
								que.push(data(enc, d.num + 1));
							}
							swap(board[i][j], board[i][j + 1]);
						}
					}
				}
			}
		}
	}
}