#include<iostream>
#include<string>
#include<map>
#include<queue>

using namespace std;

int a[8];
int dist[4] = { 1,-1,4,-4 };
map<string, int> dp;

void bfs()
{
	queue<string> q;
	string cur;
	dp["01234567"] = 0;
	q.push("01234567");
	while (!q.empty())
	{
		cur = q.front();
		q.pop();
		int p = (int)cur.find("0");
		for (int i = 0; i < 4; i++)
		{
			int np = p + dist[i];
			if (np >= 0 && np < 8 && (p != 3 || dist[i] != 1) && (p != 4 || dist[i] != -1))
			{
				string next;
				next = cur;
				next[p] = cur[np];
				next[np] = cur[p];
				if (dp.find(next) == dp.end())
				{
					dp[next] = dp[cur] + 1;
					q.push(next);
				}
			}
		}
	}
}

int main()
{
	bfs();
	while (cin >> a[0])
	{
		for (int i = 1; i < 8; i++)
			cin >> a[i];
		string s;
		for (int i = 0; i < 8; i++)
			s += a[i] + '0';
		cout << dp[s] << endl;
	}
}



