#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
#define inf 1e9
typedef pair<string,int> sp;
int dd[] = {1,-1,4,-4};


string swap(string s,int a,int b){
	char tmp = s[a];
	s[a] = s[b];
	s[b] = tmp;
	return s;
}
int main(){
string s="01234567";
map<string,int> cost;
queue<sp> q;
q.push(sp(s,0) );
cost[s] = 1;
while(!q.empty()){
	sp now = q.front();
	q.pop();
	int ss=now.second;
	//if(!cost[now.first])continue;
	for(int i=0;i<4;i++){
		if(ss+dd[i]<0||ss+dd[i]>7)continue;
		if(ss==3&&i==0)continue;
		if(ss==4&&i==1)continue;
		string next = swap(now.first,ss,ss+dd[i]);
		if(!cost[next]){
			cost[next] = cost[now.first]+1;
			q.push(sp(next,ss+dd[i]));
		}
	}

}
string st;
st.resize(8);
while(cin>>st[0]>>st[1]>>st[2]>>st[3]>>st[4]>>st[5]>>st[6]>>st[7]){
	cout<<cost[st]-1<<endl;
}


	return 0;
}

