#include<bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<int(n);i++)
struct P{
	int a[8];// don't write int a[]; will produce error
	int n;
};
int z[8] = {0,1,2,3,4,5,6,7};
#define fi first
#define se second
int d[4] = {-4,4,1,-1};
bool iseq(int a[]){
	forn(i,8){
		if(a[i]!=z[i])return false;
	}
	return true;
}
int bfs(){
	queue<P> que;
	P p;
	forn(i,8)cin>>p.a[i];
	p.n=0;
	que.push(p);
	while(que.size()){
		p = que.front();
		que.pop();
		if(iseq(p.a)) return p.n;
		int position = 0;
		while(p.a[position]!=0)position++;
		forn(i,4){
			int pos = position+d[i];
			if(pos>=0&&pos<8){
				swap(p.a[pos],p.a[position]);
				p.n++;
				que.push(p);
				p.n--;
				swap(p.a[pos],p.a[position]);
			}

		}
	}
	return -1;
}
int main(){
	while(1){
		cout<<bfs()<<endl;
	}
	return 0;
}

