#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
struct pu{int a[8]; int kaisu; int s;};//0?????????????????????????????? ?§???????????????????
queue<pu> q;
int an,wze,s2;
pu p;
int main()
{for(;;){
	pu orig;
	for(int i=0;i<8;i++) {if(scanf("%d",&orig.a[i])==EOF) return 0;}
	orig.s=-1;
	orig.kaisu=0;
	q.push(orig);
	while(1){
		p=q.front();
		s2=p.s;
		q.pop();
		for(int i=0;i<8;i++){
			if(p.a[i]!=i) break;
			if(i==7) {an=p.kaisu; goto ans;}
		}
		for(int i=0;i<8;i++) if(p.a[i]==0) wze=i;
		p.kaisu++;
		if(s2!=0&&wze>=4){//0?????????
			swap(p.a[wze],p.a[wze-4]);
			p.s=1;
			q.push(p);
			swap(p.a[wze],p.a[wze-4]);
		}
		if(s2!=1&&wze<4){//0?????????
			swap(p.a[wze],p.a[wze+4]);
			p.s=0;
			q.push(p);
			swap(p.a[wze],p.a[wze+4]);
		}
		if(s2!=2&&wze!=3&&wze!=7){//0?????????
			swap(p.a[wze],p.a[wze+1]);
			p.s=3;
			q.push(p);
			swap(p.a[wze],p.a[wze+1]);
		}
		if(s2!=3&&wze!=0&&wze!=4){//0?????????
			swap(p.a[wze],p.a[wze-1]);
			p.s=2;
			q.push(p);
			swap(p.a[wze],p.a[wze-1]);
		}
	}
	ans: printf("%d\n",an);
	while(!q.empty()) q.pop();
}}