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

using namespace std;

map<vector<int>, int> memo;
queue<vector<int>> que;
const int vec[4] = { -1,1,4,-4 };

vector<int> makev(vector<int> tv) {
	vector<int> ans(8);
	for (int i = 0; i < 8; i++)ans[i] = tv[i];
	return ans;
}

int main() {
	memo[{0, 1, 2, 3, 4, 5, 6, 7}] = 0;
	que.push({ 0,1,2,3,4,5,6,7,0 });
	while (que.size()) {
		vector<int> tv = que.front();
		que.pop();
		int zeropos;
		for (int i = 0; i < 8; i++)if (tv[i] == 0)zeropos = i;
		for (int i = 0; i < 4; i++) {
			if (i < 2) {//??????
				if (zeropos + vec[i] < 0 || zeropos + vec[i] > 7)continue;
				if ((zeropos == 3 && zeropos + vec[i] == 4) || (zeropos == 4 && zeropos + vec[i] == 3))continue;
				vector<int> tmpv = tv;
				tmpv[8]++;
				swap(tmpv[zeropos], tmpv[zeropos + vec[i]]);
				if (memo.count(makev(tmpv)) == 0) {
					memo[makev(tmpv)] = tmpv[8], que.push(tmpv);
				}
			}
			else {//??????
				if (zeropos + vec[i] < 0 || zeropos + vec[i] > 7)continue;
				vector<int> tmpv = tv;
				tmpv[8]++;
				swap(tmpv[zeropos], tmpv[zeropos + vec[i]]);
				if (memo.count(makev(tmpv)) == 0) {
					memo[makev(tmpv)] = tmpv[8], que.push(tmpv);
				}
			}
		}
	}
	vector<int> input(8);
	while (cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4] >> input[5] >> input[6] >> input[7]) {
		cout << memo[input] << endl;
	}
	return 0;
}