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

using namespace std;

typedef pair<string, int> P;

vector<string> make(string str) {
	vector<string> ret;

	int pos;
	for (int i = 0; i < str.size(); ++i)
		if (str[i] == '0') pos = i;

	vector<int> v;
	if (pos % 4 != 3) v.push_back( pos+1 );
	if (pos % 4 != 0) v.push_back( pos-1 );
	if (pos < 4) v.push_back( pos+4 );
	else v.push_back( pos-4 );

	for (int i = 0; i < v.size(); ++i) {
		string s = str;
		swap(s[pos], s[v[i]]);
		ret.push_back(s);
	}

	return ret;
}

int main() {
	while (1) {
		string str;
		for (int i = 0; i < 8; ++i) {
			int a; if ( !(cin >> a) ) return 0;
			str += a + '0';
		}

		queue<P> Q; Q.push( P(str, 0) );
		map<string, bool> m;
		while ( !Q.empty() ) {
			P p = Q.front(); Q.pop();
			string str = p.first;
			int step = p.second;

			if (m[str]) continue;
			m[str] = true;

			if (str == "01234567") {
				cout << step << endl;
				break;
			}

			vector<string> v = make(str);
			for (int i = 0; i < v.size(); ++i)
				Q.push( P(v[i], step+1) );
		}
	}
}