#include<iostream>
#include<queue>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int dir[4] = {1, -1, 4, -4};
map<string,int>res;

void BFS(){
	queue<string>que;
	que.push("01234567");
	while(!que.empty()){
		string v = que.front();
		que.pop();
		int pos = 0;
		for(int i = 0; i < 8; i++){
			if(v[i] == '0') pos = i;
			
			for(int i = 0; i < 4; i++){
				if(0 <= pos + dir[i] && pos + dir[i] < 8 &&
				 !(pos == 3 && i == 0) && !(pos == 4 && i == 1)){
				 	string u = v;
				 	swap(u[pos], u[pos + dir[i]]);
				 	
				 	if(res[u] == 0) {
				 		que.push(u);
				 		res[u] = res[v] + 1;
					 }
				 }
			}
		}
	}
}

int main(){
	int in;
	res["01234567"] = 1;
	BFS();
	while(1){
		string s;
		for(int i = 0; i < 8; i++){
			if(!(cin >> in)) return 0;
			s += in + '0';
		}
		cout << res[s] - 1 <<endl;
	}
	return 0;
}
