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

using namespace std;

typedef pair<string, int> P;

string String(vector<int> v) {
	string ret = "";
	for (int i = 0; i < v.size(); ++i)
		ret += v[i] + '0';

	return ret;
}

int main() {
	map<string, int> steps;
	queue<P> Q; Q.push( P("01234567", 0) );

	while ( !Q.empty() ) {
		P p = Q.front(); Q.pop();
		string str = p.first;
		int step = p.second;

		if (steps[str]) continue;
		steps[str] = step+1;

		int pos = -1;
		for (int i = 0; i < 8; ++i)
			if (str[i] == '0') pos = i;

		string next = str;

		if (pos > 3) swap(next[pos], next[pos-4]);
		else swap(next[pos], next[pos+4]);

		Q.push( P(next, step+1) ); next = str;

		if (pos % 4 != 0) { swap(next[pos], next[pos-1]); Q.push( P(next, step+1) ); next = str; }
		if (pos % 4 != 3) { swap(next[pos], next[pos+1]); Q.push( P(next, step+1) ); next = str; }
	}

	int a;
	while (scanf("%d", &a) != EOF) {
		vector<int> now(8, a);
		for (int i = 1; i < 8; ++i) cin >> now[i];
		cout << (steps[String(now)]-1) << endl;
	}
}