#include<iostream>
#include<queue>
#include<map>
#include<string>
using namespace std;

map<string, int> mp;
string s;
queue<pair<string, int> > Q;
const int dx[4] = {-1, 1, -4, 4};

void bfs(){
	mp["01234567"] = 0;
	Q.push(make_pair("01234567",0));
	while(!Q.empty()){
		for(int i=0;i<4;i++){
			string next = Q.front().first;
			int pos = Q.front().second;
			int npos = pos + dx[i];
			if(npos<0||npos>7||(pos==3&&npos==4)||(pos==4&&npos==3))
				continue;
			swap(next[pos],next[npos]);
			if(mp.find(next)==mp.end()){
				mp[next] = mp[Q.front().first] + 1;
				Q.push(make_pair(next,npos));
			}
		}
		Q.pop();
	}
}

int main(){
	s.resize(8);
	bfs();
	while(cin>>s[0]>>s[1]>>s[2]>>s[3]>>s[4]>>s[5]>>s[6]>>s[7]){
		cout << mp[s] << endl;
	}
}
