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

using namespace std;

string s = "01234567";
string now, net;
map <string, int> mp;
int dt[] = {1, -1, 4, -4};

void bfs() {
	
	queue <string> que;
	que.push(s);
	while(que.size()) {
		now = que.front();
		que.pop();
		
		int pos = now.find('0');
		for(int i = 0; i < 4; i++) {
			
			int tpos = pos + dt[i];
			if(tpos >= 0 && tpos < 8 && !((pos == 3 || pos == 7) && i == 0) && !((pos == 0 || pos == 4) && i == 1)) {
				net = now;
				swap(net[pos], net[tpos]);
				if(mp[net] == 0) {
					mp[net] = mp[now] + 1;
					que.push(net);
				}
			}
		}
	}
	
	return;
}
int main() {
	
	bfs();
	mp[s] = 0;
	while(getline(cin, s)) {
		s.erase(remove(s.begin(),s.end(),' '),s.end());
		cout << mp[s] << endl;
	}
	
	return 0;
}
