#define _CRT_SECURE_NO_WARNINGS
#include"bits/stdc++.h"
#ifdef _DEBUG
#define DBG(n) n
#else
#define DBG(n)
#endif
#define INF            1e9
#define INFLL          1e18
#define EPS            1e-9
#define REP(i,n)       for(ll i=0,i##_len=(n);i<i##_len;++i)
#define REP1(i,n)      for(ll i=1,i##_len=(n);i<=i##_len;++i)
#define REPR(i,n)      for(ll i=(n)-1;i>=0;--i)
#define REPR1(i,n)     for(ll i=(n);i>0;--i)
#define REPC(i,obj)    for(auto i:obj)
#define ALL(obj)       (obj).begin(),(obj).end()
#define SETP(n)        cout<<fixed<<setprecision(n)
#define VV(T,h,w)      move(vector<vector<T>>(h,vector<T>(w)))
#define VVI(T,h,w,i)   move(vector<vector<T>>(h,vector<T>(w,i)))
using namespace std;
using ll = long long;
template<typename T, ll h, ll w>using vec2 = vector<vector<T>>;
template<typename T = ll>inline T in() { T ret; cin >> ret; return ret; }

inline unsigned encode(unsigned n[2][4])
{
	unsigned ret = 0;
	REP(i, 2)REP(j, 4)ret += n[i][j] << (21 - i * 12 - j * 3);
	return ret;
}
inline void slide(unsigned c, unsigned ret[4])
{
	REP(i, 4)ret[i] = 0;
	unsigned n[2][4];
	int x, y;
	REP(i, 2) {
		REP(j, 4) {
			unsigned foo = (c >> (21 - i * 12 - j * 3)) & 0b111;
			if (foo == 0) {
				x = j;
				y = i;
			}
			n[i][j] = foo;
		}
	}
	if (x != 0) {
		swap(n[y][x], n[y][x - 1]);
		ret[0] = encode(n);
		swap(n[y][x], n[y][x - 1]);
	}
	if (x != 3) {
		swap(n[y][x], n[y][x + 1]);
		ret[1] = encode(n);
		swap(n[y][x], n[y][x + 1]);
	}
	if (y != 0) {
		swap(n[y][x], n[y - 1][x]);
		ret[2] = encode(n);
		swap(n[y][x], n[y - 1][x]);
	}
	if (y != 1) {
		swap(n[y][x], n[y + 1][x]);
		ret[3] = encode(n);
		swap(n[y][x], n[y + 1][x]);
	}
}
map<unsigned, int>push_back_all()
{
	map<unsigned, int>mp;
	queue<pair<unsigned, int>>que; que.push(make_pair(0b000001010011100101110111, 0));
	set<unsigned>appeared;
	while (!que.empty()) {
		auto q = que.front(); que.pop();
		mp[q.first] = q.second;
		unsigned sl[4];
		slide(q.first, sl);
		REP(i, 4) {
			if (sl[i] != 0) {
				if (appeared.count(sl[i]) == 0) {
					appeared.insert(sl[i]);
					que.push(make_pair(sl[i], q.second + 1));
				}
			}
		}
	}
	return mp;
}
signed main()
{
	auto mp = move(push_back_all());
	unsigned inpt[2][4];
	while (cin >> inpt[0][0] >> inpt[0][1] >> inpt[0][2] >> inpt[0][3] >> inpt[1][0] >> inpt[1][1] >> inpt[1][2] >> inpt[1][3]) {
		cout << mp[encode(inpt)] << endl;
	}
}
