#include<iostream>
#include<queue>
#include<map>
#include<vector>
using namespace std;
int a[8];
int step;

bool endsearch;
map<vector<int>, bool> visited;

bool istarget(vector<int> vec) {
	for (int i = 0; i < 8; i++) {
		if (vec[i] != i) {
			return false;
		}
	}
	return 1;
}
int pos0(vector<int> vec) {
	int i;
	for (i = 0; i < 8; i++) {
		if (vec[i] == 0)return i;
	}
}
queue<vector<int> > bfs(queue<vector<int> > que) {
	int quesize = que.size();
	step++;
	for (int i = 0; i < quesize; i++) {
		if (istarget(que.front())) {
			endsearch = 1;
			step--;
			return que;
		}
		else {
			if (!visited[que.front()]) {
				visited[que.front()] = 1;
				int position = pos0(que.front());

				vector<int> tmp(que.front());
				if (position < 4) {
					tmp[position] = que.front()[position + 4];
					tmp[position + 4] = que.front()[position];
					que.push(tmp);
				}
				else {
					tmp[position] = que.front()[position - 4];
					tmp[position - 4] = que.front()[position];
					que.push(tmp);
				}
				if (position != 0 && position != 4) {
					vector<int> tmp1(que.front());
					tmp1[position] = que.front()[position-1];
					tmp1[position -1] = que.front()[position];
					que.push(tmp1);
				}
				if (position != 3 && position != 7) {
					vector<int> tmp1(que.front());
					tmp1[position] = que.front()[position + 1];
					tmp1[position + 1] = que.front()[position];
					que.push(tmp1);
				}

				que.pop();
			}
			else {
				que.pop();
			}
		}
	}
	return que;
}


int main() {
	while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
		vector<int> vec(a, a + 8);
		queue<vector<int> > que;
		visited.clear();
		step = 0;
		endsearch = 0;
		que.push(vec);
		while (que.size() != 0) {
			que = bfs(que);
			if (endsearch) {
				cout << step << endl;
				break;
			}
		}
	}
	//system("pause");
	return 0;
}