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

using namespace std;

int Dis[3];

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 Change_CtoI(char c) {
	return (int)c - (int)'0';
}

int Cal_Cost(string str) {
	int ret = 0;
	for (int i = 0; i < 8; ++i) {
		if ((int)str[i] < 4) {
			if (i < 4) {
				ret += abs(Change_CtoI(str[i]) - i);
			}
			else {
				ret += abs(Change_CtoI(str[i]) - i) + 1;
			}
		}
		else {
			if (i >= 4) {
				ret += abs(Change_CtoI(str[i]) - i);
			}
			else {
				ret += abs(Change_CtoI(str[i]) - i) + 1;
			}
		}
	}
	return ret;
}

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

		if (str == "01234567") {
			cout << 0 << endl;
		}
		else {
			int ret,cn,mc,cost;
			bool find = false;
			string ss;
			queue<string> puzz;
			queue<int> count;
			queue<int> index;
			puzz.push(str);
			count.push(0);
			index.push(ind);
			map<string,bool> ob;
			ob[str] = true;
			while (!puzz.empty() && !find) {
				str = puzz.front();
				ind = index.front();
				cn = count.front();
				puzz.pop();
				count.pop();
				index.pop();
				Set_Movable(ind);
				mc = Cal_Cost(str);
				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;
							cost = Cal_Cost(ss);
							if (mc + 6 >= cost) {
								if (mc > cost) {
									mc = cost;
								}
								puzz.push(ss);
								count.push(cn+1);
								index.push(Dis[i]);
							}
						}
						if (ss == "01234567") {
							ret = cn+1;
							find = true;
						}
					}
				}
			}
			cout << ret << endl;
		}
	}
	return 0;
}