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

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

using namespace std;

#define L 8

int mv[4] = {1, -1, 4, -4};

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

	map<string, int> state;
	state[start] = 0;
	queue<string> q;
	q.push(start);

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

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

		unsigned 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;
				u.replace(zPos, 1, t.substr((unsigned int)nmv, 1));
				u.replace((unsigned int)nmv, 1, "0");

				if (!state.count(u)) {
					q.push(u);
					state[u] = state[t] + 1;
				}
			}
		}
	}

	return res;		
}


int main()
{

	int i, j;
	string start;
	string goal = "01234567";

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

	return 0;
}