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

typedef struct node {
	int seq[8];
	int id;
	int s;
	int step;
} item;

int idBox[40400];
int idCnt;

int dmove[] = {-1, -4, +1, +4};
int ans[] = {0,1,2,3,4,5,6,7};

int bfs(item n);
bool isUnique(int id);


int main() {
	while (1) {
		item start;
		start.step = 0;
		
		for (int i = 0; i<8; i++) {
			if (scanf("%d", &start.seq[i])==EOF) return 0;
			if (start.seq[i]==0) start.s = i;
		}
		for (int i = 0; i<8; i++)
			start.id = start.id*10+start.seq[i];
		isUnique(start.id);
		
		cout<<bfs(start)<<endl;
		
		
		memset(idBox, 0, sizeof(idBox));
		idCnt = 0;
	}
	
	return 0;
}


bool isUnique(int id) {
	//??????idBox????????????id??????????°±??????id??°idBox???

	for (int i = 0; i<idCnt; i++)
		if (id==idBox[i]) return false;
	idBox[idCnt++] = id;
	return true;
}

int bfs(item n) {
	queue<item> Q;
	Q.push(n);
	
	while (Q.size()) {
		item now = Q.front();
		Q.pop();
		if (memcmp(ans, now.seq, sizeof(ans))==0)
			return now.step;
		
		for (int i = 0; i<4; i++) {
			int ns = now.s + dmove[i];
			if (ns>=0 && ns<=7 && !(now.s==3 && ns==4) && !(now.s==4 && ns==3)) {
				item tmp;//???????????¨????????????????????°????????????????\????
				memset(&tmp, 0, sizeof(tmp));
				for (int i = 0; i<8; i++)
					tmp.seq[i] = now.seq[i];
				tmp.seq[ns] = now.seq[now.s];
				tmp.seq[now.s] = now.seq[ns];

				//?????¢0???ns???????????°??????
				for (int i = 0; i<8; i++) 
					tmp.id = tmp.id*10+tmp.seq[i];
				//??????id
				tmp.s = ns; tmp.step = now.step+1;
				//??????????????¶????????°
				if (isUnique(tmp.id)) Q.push(tmp);
			}
		}
		
	}
	return -1;
}