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

#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()
{
	vector<int> goal;
	for (int i = 0; i < N; ++i) goal.pb(i);
	while (fgets(buf, BUFSIZE, stdin) != NULL) {
		vector<int> v;
		char* p = strtok(buf, " ");
		while (p != NULL) { v.pb(*p - '0'); p = strtok(NULL, " "); }

		queue<vector<int> > q;
		map<vector<int>, int> m;
		q.push(v);
		m[v] = 0;
		int ans = 0;
		while (!q.empty()) {
			vector<int> t = q.front(); q.pop();
			if (t == goal) { ans = m[t]; break; }
			int z = find(all(t), 0) - t.begin();
			for (int i = 0; i < 4; ++i) {
				int k = z + d[i];
				if (k < 0 || k > N-1) continue;
				vector<int> next = t;
				swap(next[z], next[k]);
				if (m.find(next) == m.end()) {
					m[next] = m[t] + 1;
					q.push(next);
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}