#include "bits/stdc++.h"
using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

//#define int long long
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct state
{
	int puzzle[8];
	int depth;
	int zp;
	int val;
	bool operator==(const state& s)const
	{
		rep(i, 0, 8)
		{
			if (puzzle[i] != s.puzzle[i])
			{
				return false;
			}
		}
		return true;
	}
	bool operator<(const state& s)const
	{
		return val < s.val;
	}
	bool operator>(const state& s)const
	{
		return val > s.val;
	}
};

vector<state> cs;
state goal;

bool checked(state s)
{
	return find(all(cs), s) != cs.end();
}

int manhattan(state s)
{
	map<int, int> goalPos,sPos;
	rep(i, 0, 8)goalPos[i] = i;
	rep(i, 0, 8)rep(j, 0, 8)
	{
		if (s.puzzle[j] == i)
		{
			sPos[i] = j;
		}
	}
	int val = 0;
	rep(i, 0, 8)
	{
		val += (abs(goalPos[i] / 4 - sPos[i] / 4) + abs(goalPos[i] % 4 - sPos[i] % 4));
	}
	return val;
}

int solve(state start)
{
	priority_queue<state, vector<state>, greater<state>> q;
	q.push(start);

	while (true)
	{
		state ts = q.top(); q.pop();
		if (ts == goal)
		{
			return ts.depth;
		}
		if (ts.zp % 4 != 0)
		{
			state s = ts;
			swap(s.puzzle[s.zp], s.puzzle[s.zp - 1]);
			s.zp--;
			s.depth++;
			s.val = manhattan(s) + s.depth;
			if (!checked(s))
			{
				cs.push_back(s);
				q.push(s);
			}
		}
		if (ts.zp % 4 != 3)
		{
			state s = ts;
			swap(s.puzzle[s.zp], s.puzzle[s.zp + 1]);
			s.zp++;
			s.depth++;
			s.val = manhattan(s) + s.depth;
			if (!checked(s))
			{
				cs.push_back(s);
				q.push(s);
			}
		}
		if (ts.zp / 4 == 0)
		{
			state s = ts;
			swap(s.puzzle[s.zp], s.puzzle[s.zp + 4]);
			s.zp+=4;
			s.depth++;
			s.val = manhattan(s) + s.depth;
			if (!checked(s))
			{
				cs.push_back(s);
				q.push(s);
			}
		}
		if (ts.zp / 4 == 1)
		{
			state s = ts;
			swap(s.puzzle[s.zp], s.puzzle[s.zp - 4]);
			s.zp-=4;
			s.depth++;
			s.val = manhattan(s) + s.depth;
			if (!checked(s))
			{
				cs.push_back(s);
				q.push(s);
			}
		}
	}
}

signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	rep(i, 0, 8)goal.puzzle[i] = i;

	int A[8],zp;
	while (true)
	{
		rep(i, 0, 8)
		{
			cin >> A[i];
			if (A[i] == 0)
			{
				zp = i;
			}
		}
		state s;
		rep(i, 0, 8)
		{
			s.puzzle[i] = A[i];
		}
		s.depth = 0;
		s.zp = zp;
		s.val = manhattan(s);
		cout << solve(s) << endl;
	}

	return 0;
}
