#include <cstdio>
#include <queue>
#include <utility>
#include <map>
using namespace std;
typedef pair<unsigned int,int> P;
#define AT(X,Y)(((X)>>((Y)*3))&7)
unsigned int F(unsigned int x,int y,int z) {
	unsigned int tmp=0;
	for(int i=0;i<8;i++) if(i!=y&&i!=(y+z))tmp|=(AT(x,i)<<(i*3));
	tmp|=(AT(x,y)<<(3*(y+z)));
	tmp|=(AT(x,y+z)<<(3*y));
	return tmp;
}
int main() {
	unsigned int start=076543210;
	map<unsigned,int> M;
	M[start]=0;
	queue<P> Q;
	Q.push(P(start,0));
	while(!Q.empty()) {
		P p=Q.front();Q.pop();
		int zero;
		for(zero=0;AT(p.first,zero);zero++);
		if(zero%4!=0) {
			unsigned int tmp=F(p.first,zero,-1);
			if(M.find(tmp)==M.end()) { M[tmp]=p.second+1; Q.push(P(tmp,p.second+1)); }
		}
		if(zero%4!=3) {
			unsigned int tmp=F(p.first,zero,1);
			if(M.find(tmp)==M.end()) { M[tmp]=p.second+1; Q.push(P(tmp,p.second+1)); }
		}
		if(zero<=3) {
			unsigned int tmp=F(p.first,zero,4);
			if(M.find(tmp)==M.end()) { M[tmp]=p.second+1; Q.push(P(tmp,p.second+1)); }
		}else {
			unsigned int tmp=F(p.first,zero,-4);
			if(M.find(tmp)==M.end()) { M[tmp]=p.second+1; Q.push(P(tmp,p.second+1)); }
		}
	}
	int n,m;
	while(scanf("%d",&n)!=EOF) {
		m=n;
		for(int i=1;i<8;i++) {
			scanf("%d",&n);
			m|=((n)<<(i*3));
		}
		printf("%d\n",M[m]);
	}
}