#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>

typedef long long ll;

using namespace std;


int main()
{
	while (true)
	{
		char buf[128];
		cin.getline(buf, sizeof(buf));
		if (strlen(buf) == 0)
			break;
		istringstream is(buf);
		int p = 0;
		for (int i = 0; i < 8; ++i)
		{
			int t;
			is >> t;
			p = p*10 + t;
		}

		int ans = -1;
		if (p == 1234567)
			ans = 0;

		map<int, bool> visited;
		vector<int> q;
		q.push_back(p);
		for (int i = 0; ans == -1; ++i)
		{
			vector<int> v;
			for (int j = 0; j < q.size(); ++j)
				v.push_back(q[j]);
			q.clear();

			for (int j = 0; j < v.size(); ++j)
			{
				int t = v[j];
				//printf("%d\n", t);

				int puzzle[8];
				for (int k = 7; k >= 0; --k, t /= 10)
					puzzle[k] = t % 10;

				int zeroPos;
				for (zeroPos = 0; puzzle[zeroPos]; ++zeroPos)
					;
				const int d[] = { -4, 4, -1, 1 };
				for (int k = 0; k < 4; ++k)
				{
					int swapPos = zeroPos + d[k];
					if (swapPos < 0 || swapPos >= 8 ||
						zeroPos == 3 && d[k] == 1 || zeroPos == 4 && d[k] == -1)
						continue;

					int copy[8];
					memcpy(copy, puzzle, sizeof(copy));
					swap(copy[zeroPos], copy[swapPos]);
					int nextPuzzle = 0;
					for (int l = 0; l < 8; ++l)
						nextPuzzle = nextPuzzle*10 + copy[l];

					if (nextPuzzle == 1234567)
					{
						ans = i + 1;
						break;
					}
					else if (!visited[nextPuzzle])
					{
						q.push_back(nextPuzzle);
						visited[nextPuzzle] = true;
					}
				}
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}