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

typedef pair<int*, int> p;
queue<p> que;
#define rep(a) for(int a = 0; a < 8; a++)

int main(){
	int dat[8];
	bool checked[8][8][8][8][8][8][8][8];
	int* con;
	p P;
	int cou, zero;
	int a, b, c, d, e, f, g, h;
	
	//printf("a");
	
	while((scanf("%d%d%d%d%d%d%d%d", &dat[0], &dat[1], &dat[2], &dat[3], &dat[4], &dat[5], &dat[6], &dat[7])) != EOF){
		P.first = dat; P.second = 0;
		que.push(P);
		
		rep(a) rep(b) rep(c) rep(d) rep(e) rep(f) rep(g) rep(h) checked[a][b][c][d][e][f][g][h] = false;
		
		while(!que.empty()){
			P = que.front(); que.pop();
			
			con = P.first;
			cou = P.second;
			
			for(int i = 0; i < 8; i++){
				if(con[i] != i) break;
				if(i + 1 == 8){
					printf("%d\n", cou);
					goto OUT;
				}
			}
			
			for(int i = 0; i < 8; i++){
				if(con[i] == 0){
					zero = i;
					break;
				}
			}
			
			
			checked[con[0]][con[1]][con[2]][con[3]][con[4]][con[5]][con[6]][con[7]] = true;
			switch(zero % 4){
				case 0 : 
					if(!checked[con[4]][con[1]][con[2]][con[3]][con[0]][con[5]][con[6]][con[7]]){
						swap(con[0], con[4]);
						P.first = con; P.second = cou + 1;
						que.push(P);
						swap(con[0], con[4]);
					}
					break;
				case 1 : 
					if(!checked[con[0]][con[5]][con[2]][con[3]][con[4]][con[1]][con[6]][con[7]]){
						swap(con[1], con[5]);
						P.first = con; P.second = cou + 1;
						que.push(P);
						swap(con[1], con[5]);
					}
					break;
				case 2 : 
					if(!checked[con[0]][con[1]][con[6]][con[3]][con[4]][con[5]][con[2]][con[7]]){
						swap(con[2], con[6]);
						P.first = con; P.second = cou + 1;
						que.push(P);
						swap(con[2], con[6]);
					}
					break;
				case 3 : 
					if(!checked[con[0]][con[1]][con[2]][con[7]][con[4]][con[5]][con[6]][con[3]]){
						swap(con[3], con[7]);
						P.first = con; P.second = cou + 1;
						que.push(P);
						swap(con[3], con[7]);
					}
					break;
				default : 
					break;
			}
			if(zero == 0 || zero == 1){
				if(!checked[con[1]][con[0]][con[2]][con[3]][con[4]][con[5]][con[6]][con[7]]){
					swap(con[0], con[1]);
					P.first = con; P.second = cou + 1;
					que.push(P);
					swap(con[0], con[1]);
				}
			}
			if(zero == 1 || zero == 2){
				if(!checked[con[0]][con[2]][con[1]][con[3]][con[4]][con[5]][con[6]][con[7]]){
					swap(con[1], con[2]);
					P.first = con; P.second = cou + 1;
					que.push(P);
					swap(con[1], con[2]);
				}
			}
			if(zero == 2 || zero == 3){
				if(!checked[con[0]][con[1]][con[3]][con[2]][con[4]][con[5]][con[6]][con[7]]){
					swap(con[2], con[3]);
					P.first = con; P.second = cou + 1;
					que.push(P);
					swap(con[2], con[3]);
				}
			}
			if(zero == 4 || zero == 5){
				if(!checked[con[0]][con[1]][con[2]][con[3]][con[5]][con[4]][con[6]][con[7]]){
					swap(con[4], con[5]);
					P.first = con; P.second = cou + 1;
					que.push(P);
					swap(con[4], con[5]);
				}
			}
			if(zero == 5 || zero == 6){
				if(!checked[con[0]][con[1]][con[2]][con[3]][con[4]][con[6]][con[5]][con[7]]){
					swap(con[5], con[6]);
					P.first = con; P.second = cou + 1;
					que.push(P);
					swap(con[5], con[6]);
				}
			}
			if(zero == 6 || zero == 7){
				if(!checked[con[0]][con[1]][con[2]][con[3]][con[4]][con[5]][con[7]][con[6]]){
					swap(con[6], con[7]);
					P.first = con; P.second = cou + 1;
					que.push(P);
					swap(con[6], con[7]);
				}
			}
		}
		OUT://out of routine
	}
	
	return 0;
}