#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>

using namespace std;

int main() {
	queue<string> que;
	map<string,int> mp;
	int d[4]={1,4,-1,-4};

	que.push("01234567");
	string tmp;
	while(!que.empty()) {
		string str=que.front();
		que.pop();
		int zero=str.find("0");
		for(int i=0;i<4;i++) {
			if(zero%4==3&&i==0) continue;
			if(zero%4==0&&i==2) continue;
			if(0>(zero+d[i])||7<(zero+d[i])) continue;
			tmp=str,swap(tmp[zero],tmp[zero+d[i]]);
			if(mp.find(tmp)!=mp.end()) continue;
			mp[tmp]=mp[str]+1;
			que.push(tmp);
		}
	}
	while(getline(cin,tmp)) {
		size_t pos;
		while((pos=tmp.find(" "))!=string::npos) tmp.erase(pos,1);
		cout<<mp[tmp]<<endl;
	}
}