#include <cstdio>
#include <utility>
#include <queue>
#include <map>

using namespace std;

#define AT(X,Y) ((X)>>(7-Y)*3&7)

typedef pair<int,int> P;

int main() {
	queue<P> Q;
	map<int,int> mp;
	int d[]={1,-1,4,-4};
	Q.push(P(01234567,0));
	while(!Q.empty()) {
		P p=Q.front();Q.pop();
		for(int i=0;i<4;i++) {
			int nd=p.second+d[i];
			if((p.second%4==3&&i==0)||(p.second%4==0&&i==1)||(0>nd)||(nd>7)) continue;
			int v=0;
			for(int j=0;j<=7;j++) v<<=3,v|=(j==nd)?AT(p.first,p.second):(j==p.second)?AT(p.first,nd):AT(p.first,j);
			if(mp.find(v)!=mp.end()) continue;
			Q.push(P(v,nd));
			mp[v]=mp[p.first]+1;
		}
	}
	while(1) {
		int v=0;
		for(int i=0;i<8;i++) {
			int d=0;
			if(scanf("%d",&d)==EOF) return 0;
			v<<=3,v|=d;
		}
		printf("%d\n",mp[v]);
	}
}