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

using namespace std;

map < vector <int>, int > memo, visited;
vector <int> puzzle;
vector <int> dist[8];
int a;

bool check(vector <int> vi)
{
	for (int i = 0; i < vi.size(); i++){
		if (vi[i] != i) return false;
	}
	return true;
}

int calc(vector <int> puzzle, int deep)
{
	if (memo[puzzle]) return memo[puzzle];

	queue < pair < vector <int>, int> > q;
	q.push(make_pair(puzzle, deep));

	while (!q.empty()){
		pair <vector <int>, int> now = q.front();
		vector <int> puzzle = now.first;
		q.pop();
		if (check(puzzle)) return memo[puzzle] = now.second;

		visited[puzzle] = true;

		for (int i = 0; i < 8; i++){
			if (puzzle[i] == 0){
				for (int j = 0; j < dist[i].size(); j++){
					swap(puzzle[i], puzzle[dist[i][j]]);
					if (visited[puzzle]) {
						swap(puzzle[i], puzzle[dist[i][j]]);
						continue;
					}
					q.push(make_pair(puzzle, now.second + 1));
					swap(puzzle[i], puzzle[dist[i][j]]);
				}
			}
		}
	}

	return -1;
}

void init(void)
{
	dist[0].push_back(1);
	dist[0].push_back(4);
	
	dist[1].push_back(0);
	dist[1].push_back(2);
	dist[1].push_back(5);
	
	dist[2].push_back(1);
	dist[2].push_back(3);
	dist[2].push_back(6);
	
	dist[3].push_back(2);
	dist[3].push_back(7);

	dist[4].push_back(0);
	dist[4].push_back(5);

	dist[5].push_back(1);
	dist[5].push_back(4);
	dist[5].push_back(6);

	dist[6].push_back(2);
	dist[6].push_back(5);
	dist[6].push_back(7);

	dist[7].push_back(3);
	dist[7].push_back(6);
}

int main(void)
{
	init();
	while (1){
		puzzle.clear();
		visited.clear();
		for (int i = 0; i < 8; i++){
			cin >> a;
			puzzle.push_back(a);
		}
		// cout << "inputed" << endl;
		if (cin.eof()) return 0;

		cout << calc(puzzle, 0) << endl;
	}

	return 0;
}