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

const int INF = 2100000000;
int dx[] = {
	1, 0, -1, 0
};
int dy[] = {
	0, 1, 0, -1
};
int puzzle[8][8][8][8][8][8][8][8];
struct pattern{
	int x, y;
	int p[2][4];
};
void swap(int p[2][4], int x1,int y1, int x2, int y2){
	int t = p[y1][x1];
	p[x1][y1] = p[y2][x2];
	p[y2][x2] = t;
}

int main(){
	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++){
									puzzle[i][j][k][l][m][n][o][p] = INF;
								}
							}
						}
					}
				}
			}
		}
	}
	puzzle[0][1][2][3][4][5][6][7] = 0;
	pattern pat;
	for(int i = 0; i < 8; i++){
		pat.p[i/4][i%4] = i;
	}
	pat.x = 0;
	pat.y = 0;
	queue<pattern> qp;
	qp.push(pat);
	while(!qp.empty()){
		pat = qp.front();
		qp.pop();
		int x = pat.x;
		int y = pat.y;
		int from = puzzle[pat.p[0][0]][pat.p[0][1]][pat.p[0][2]][pat.p[0][3]]
						[pat.p[1][0]][pat.p[1][1]][pat.p[1][2]][pat.p[1][3]];
		for(int i = 0; i < 4; i++){
			if(0 <= x+dx[i] && x+dx[i] < 2 && 0 <= y+dy[i] && y+dy[i] < 4){
				pattern pa;
				pa.x = x+dx[i];
				pa.y = y+dy[i];
				for(int j = 0; j < 2; j++){
					for(int k = 0; k < 2; k++){
						pa.p[j][k] = pat.p[j][k];
					}
				}
				swap(pa.p, x, y, pa.x, pa.y);
				int to = puzzle[pa.p[0][0]][pa.p[0][1]][pa.p[0][2]][pa.p[0][3]]
						[pa.p[1][0]][pa.p[1][1]][pa.p[1][2]][pa.p[1][3]];
				if(to == INF){
					qp.push(pa);
					puzzle[pa.p[0][0]][pa.p[0][1]][pa.p[0][2]][pa.p[0][3]]
						[pa.p[1][0]][pa.p[1][1]][pa.p[1][2]][pa.p[1][3]] = from + 1;
				}
			}
		}
	}
	int a, b, c, d, e, f, g, h;
	while(cin >> a >> b >> c >> d >> e >> f >> g >> h){
		cout << puzzle[a][b][c][d][e][f][g][h] << endl;
	}
	return 0;
}