#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef vector<int> vi;

set<vi> visited;
priority_queue<pair<int,vi>,vector<pair<int,vi> >,greater<pair<int,vi> > > next;
vi final(8);

int visit(vi &t,int s){
	
	if(t==final)return 1;
	if(visited.find(t)!=visited.end())return 0;
	visited.insert(t);
	next.push(make_pair(s+1,t));
	return 0;
}
int main(){
	vi p(8),t(8),ct(8);
	rep(i,8)final[i]=i; sort(final.begin(),final.end());
	
	while(cin>>p[0]){
		rep(i,7)cin>>p[i+1];
		visited.clear(); while(!next.empty())next.pop();

		int c=0; if(p==final)goto END;
		next.push(make_pair(0,p));
		while(!next.empty()){
			int step=next.top().first,z,f=0;
			t=next.top().second; next.pop();
			rep(i,8)if(t[i]==0)z=i;
			
			if(z%4!=3)ct=t,swap(ct[z],ct[z+1]),f=f||visit(ct,step);
			if(z%4)ct=t,swap(ct[z],ct[z-1]),f=f||visit(ct,step);
			if(z<4)ct=t,swap(ct[z],ct[z+4]),f=f||visit(ct,step);
			if(z>3)ct=t,swap(ct[z],ct[z-4]),f=f||visit(ct,step);
			if(f){
				c=step+1; break;
			}
		}
		END:;
		cout<<c<<endl;
	}
	return 0;
}