#include <iostream>
#include <string>
#include <queue>
#include <map>
#define FOR(a,start,end) for(I64 a=(start);a<(end);a++)
using namespace std;
int main(){
	int vec[4] = {-1, 1, 4, -4};
	queue<string> que;
	que.push("01234567");
	map<string, int> d;
	d["01234567"] = 0;
	while(!que.empty()){
		string str = que.front();
		que.pop();
		int zero = str.find('0');
		FOR(i, 0, 4){
			int next_z;
			next_z = zero + vec[i];
			if(next_z >= 0 && next_z <= 7){
				string str2 = str;
				swap(str2[zero], str2[next_z]);
				if(d.find(str2) == d.end()){
					que.push(str2);
					d[str2] = d[str] + 1;
				}
			}
		}
	}
	string num;
	while(getline(cin, num)){
		string::iterator end_it = remove(num.begin(), num.end(), ' ');
		num.erase(end_it, num.end());
		cout << d[num] << endl;
	}
}