#include <stdio.h>
#include <stdlib.h>

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

using namespace std;

#define L 8

map<string, int> state;
int mv[4] = {1, -1, 4, -4};

int bfs(string start, string goal)
{
	int res = 0;
	int i, j;

//	map<string, int> state;
	queue<string> q;
	map<string, bool> passed;
	q.push(start);
	passed[start] = true;

//cout << "start:" << start << '\n';
//cout << "goal: " << goal  << '\n';

	while (q.size()) {
		
		string t = q.front();
		q.pop();

		if (t == goal) {
			res = state[t];

//cout << "Found goal:" << t << '\n';

			break;	
		}

		int zPos = t.find("0");

		for (i=0; i<4; i++) {
			int nmv = zPos + mv[i];
			if (0<=nmv && nmv<L && !(zPos == 4 && nmv == 3) && !(zPos == 3 && nmv == 4)) {
				string u = t;
				swap(u[zPos], u[nmv]);

//				if (!state.count(u)) {
				if (!passed[u]) {
					q.push(u);
					passed[u] = true;

					if (!state.count(u))
						state[u] = state[t] + 1;

//cout << "pushed:" << u << '\n';
				}
			}
		}
	}

	return res;		
}


int main()
{

	int i, j;
	string start = "01234567";
	string goal;
	state[start] = 0;

	while (1) {
//cout << "-----------------------------" << '\n';
		//input
		char c;
		if (scanf("%c ", &c) == EOF) break;
		goal = c;
		for (i=1; i<L; i++) {
			scanf("%c ", &c);
			goal += c;
		}
	
		//bfs & output
		printf("%d\n", bfs(start, goal));
	}	

	return 0;
}