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

using namespace std;

int Dis[3];

int Check_Zero_Index(string str) {
	for (int i = 0; i < 8; ++i) {
		if (str[i] == '0') {
			return i;
		}
	}
	return 0;
}

void Set_Movable(int ind) {
	if (ind%4 == 0) {
		Dis[0] = -1;
		Dis[1] = ind+1;
	}
	else if (ind%4 == 3) {
		Dis[0] = ind-1;
		Dis[1] = -1;
	}
	else {
		Dis[0] = ind-1;
		Dis[1] = ind+1;
	}
	Dis[2] = (ind+4)%8;
}

int main(void) {
	string temp;
	while (getline(cin,temp)) {
		string str = "";
		for (int i = 0; i < 8; ++i) {
			str += temp[2*i];
		}

		int ret,ind,cn;
		bool find = false;
		string ss;
		queue<string> puzz;
		queue<int> count;
		puzz.push(str);
		count.push(0);
		map<string,bool> ob;
		ob[str] = true;
		if (str == "01234567") {
			cout << 0 << endl;
		}
		else {
			while (!find) {
				str = puzz.front();
				ind = Check_Zero_Index(str);
				cn = count.front();
				puzz.pop();
				count.pop();
				Set_Movable(ind);
				for (int i = 0; i < 3; ++i) {
					ss = str;
					if (!(Dis[i] < 0)) {
						swap(ss[ind],ss[Dis[i]]);
						if (!ob[ss]) {
							ob[ss] = true;
							puzz.push(ss);
							count.push(cn+1);
						}
						if (ss == "01234567") {
							ret = cn+1;
							find = true;
						}

					}
				}
			}
			cout << ret << endl;
		}
	}
	return 0;
}