#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <string>

#define pb push_back
#define all(c) (c).begin(), (c).end()

using namespace std;

#define N 8
#define BUFSIZE 32
char buf[BUFSIZE];
int d[4] = { 4, -4, 1, -1 };

int main()
{
	const char* goal = "01234567";
	while (fgets(buf, BUFSIZE, stdin) != NULL) {
		char v[N+1];
		char* p = strtok(buf, " "), *c = v;
		while (p != NULL) { *c++ = *p; p = strtok(NULL, " "); }
		*c = '\0';

		queue<string> q;
		map<string, int> m;
		q.push(string(v));
		m[string(v)] = 0;
		int ans = 0;
		while (!q.empty()) {
			char t[N+1];
			strcpy(t, q.front().c_str());
			q.pop();
			if (!strcmp(t, goal)) { ans = m[t]; break; }
			int z;
			for (z = 0; z < N; ++z) if (t[z] == '0') break;
			for (int i = 0; i < 4; ++i) {
				int k = z + d[i];
				if (k < 0 || k > 7) continue;
				if (z == 3 && k == 4 || z == 4 && k == 3) continue;
				char next[N+1];
				strcpy(next, t);
				swap(next[z], next[k]);
				if (m.find(next) == m.end()) {
					m[string(next)] = m[string(t)] + 1;
					q.push(string(next));
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}