#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;
}

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;
				for (int j = 0; j < 7 ; j++) {
					if (j == 3)continue;
					vector<int> tmp(que.front());
					tmp[j] = que.front()[j + 1];
					tmp[j + 1] = que.front()[j];
					que.push(tmp);
				}
				for (int j = 0; j < 4; j++)
				{
					vector<int> tmp(que.front());
					tmp[j] = que.front()[j + 4];
					tmp[j + 4] = que.front()[j];
					que.push(tmp);
				}
				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;
			}
		}
	}
	return 0;
}