#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

const int N=8;
map<string,int> mp;
int d[4]={-1,1,4,-4};

void bfs(){
	
	queue<string> q;
	q.push("01234567");
	
	while(q.size()){
		string t=q.front();
		q.pop();
		int p=t.find('0');
		for(int i=0;i<=3;i++){
			int np=p+d[i];
			if(p<0 || p>7 || ((p==3 || p==7) && i==1) || ((p==0 || p==4) && i==0)) continue;
			string n=t;
			swap(n[np],n[p]);
			if(mp[n]==0){
				mp[n]=mp[t]+1;
				q.push(n);
			}
		}
	}
	
}


int main(){
	bfs();
	mp["01234567"]=0;
	int a[N];
	while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]){
		string s;
		for(int i=0;i<8;i++)
			s+=a[i]+'0';
		cout<<mp[s]<<endl;
	}
	return 0;
}
