#include <iostream>
#include <algorithm> 
#include <queue>
using namespace std;

class SP{
public:
	int p[2][4];
	int n;
	bool seven_puzzle(){
		return p[0][0] == 0 && p[0][1] == 1 && p[0][2] == 2 && p[0][3] == 3
			&& p[1][0] == 4 && p[1][1] == 5 && p[1][2] == 6 && p[1][3] == 7;
	}

	void rot1(int j0){
		swap(p[0][j0], p[1][j0]);
	}
	void rot2(int j0){
		swap(p[0][j0], p[1][j0]);
	}
	void rot3(int i0,int j0){
		swap(p[i0][j0], p[i0][j0-1]);
	}
	void rot4(int i0,int j0){
		swap(p[i0][j0], p[i0][j0+1]);
	}
};

int main(){
	while (1){
		SP sp;
		sp.n = 0;
		int i0 = 0;
		int j0 = 0;
		cin >> sp.p[0][0];
		if (cin.eof())
			break;
		for (int i = 0; i < 2; i++){
			for (int j = 1; j < 4; j++){
				cin >> sp.p[i][j];
			}
		}
		bool memo[8][8][8][8][8][8][8];
		for (int a = 0; a < 8; a++){
			for (int b = 0; b < 8; b++){
				for (int c = 0; c < 8; c++){
					for (int d = 0; d < 8; d++){
						for (int e = 0; e < 8; e++){
							for (int f = 0; f < 8; f++){
								for (int g = 0; g < 8; g++){
									memo[a][b][c][d][e][f][g] = false;
								}
							}
						}
					}
				}
			}
		}
		memo[sp.p[0][0]][sp.p[0][1]][sp.p[0][2]][sp.p[0][3]][sp.p[1][0]][sp.p[1][1]][sp.p[1][2]] = true;
		queue<SP> que;
		que.push(sp);
		while (!que.empty()){
			sp = que.front();
			que.pop();
			/*if (sp.seven_puzzle()){
				cout << sp.n << endl;
				continue;
			}*/

			for (int i = 0; i < 2; i++){
				for (int j = 0; j < 4; j++){
					if (sp.p[i][j] == 0){
						i0 = i;
						j0 = j;
					}
				}
			}

			SP sp1 = sp;
			if (i0 == 0){
				sp1.rot1(j0);
				sp1.n++;
				if (sp1.seven_puzzle()){
					cout << sp1.n << endl;
					break;
				}
				if (memo[sp1.p[0][0]][sp1.p[0][1]][sp1.p[0][2]][sp1.p[0][3]][sp1.p[1][0]][sp1.p[1][1]][sp1.p[1][2]] == false){
					memo[sp1.p[0][0]][sp1.p[0][1]][sp1.p[0][2]][sp1.p[0][3]][sp1.p[1][0]][sp1.p[1][1]][sp1.p[1][2]] = true;
					que.push(sp1);
				}
			}

			SP sp2 = sp;
			if (i0 == 1){
				sp2.rot2(j0);
				sp2.n++;
				if (sp2.seven_puzzle()){
					cout << sp2.n << endl;
					break;
				}
				if (memo[sp2.p[0][0]][sp2.p[0][1]][sp2.p[0][2]][sp2.p[0][3]][sp2.p[1][0]][sp2.p[1][1]][sp2.p[1][2]] == false){
					memo[sp2.p[0][0]][sp2.p[0][1]][sp2.p[0][2]][sp2.p[0][3]][sp2.p[1][0]][sp2.p[1][1]][sp2.p[1][2]] = true;
					que.push(sp2);
				}
			}

			SP sp3 = sp;
			if (j0 != 0){
				sp3.rot3(i0, j0);
				sp3.n++;
				if (sp3.seven_puzzle()){
					cout << sp3.n << endl;
					break;
				}
				if (memo[sp3.p[0][0]][sp3.p[0][1]][sp3.p[0][2]][sp3.p[0][3]][sp3.p[1][0]][sp3.p[1][1]][sp3.p[1][2]] == false){
					memo[sp3.p[0][0]][sp3.p[0][1]][sp3.p[0][2]][sp3.p[0][3]][sp3.p[1][0]][sp3.p[1][1]][sp3.p[1][2]] = true;
					que.push(sp3);
				}
			}

			SP sp4 = sp;
			if (j0 != 3){
				sp4.rot4(i0, j0);
				sp4.n++;
				if (sp4.seven_puzzle()){
					cout << sp4.n << endl;
					break;
				}
				if (memo[sp4.p[0][0]][sp4.p[0][1]][sp4.p[0][2]][sp4.p[0][3]][sp4.p[1][0]][sp4.p[1][1]][sp4.p[1][2]] == false){
					memo[sp4.p[0][0]][sp4.p[0][1]][sp4.p[0][2]][sp4.p[0][3]][sp4.p[1][0]][sp4.p[1][1]][sp4.p[1][2]] = true;
					que.push(sp4);
				}
			}

		}
		return 0;
	}

}