#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;

string exchange(int a, int b, string s)
{
	char tmp;
	 tmp = s[a];
	s[a] = s[b];
	s[b] = tmp ;
	return s;
}


int main()
{
	string first;
	queue<string> numbers;
	map<string, int> step;
	
	while(!cin.eof())
	{
		first = "01234567";
		
		for(int i = 0; i < 8; ++i)
		{
			int tmp;
			cin >> tmp;
			first[i] = tmp + '0';
		}
		
		while(!numbers.empty()) numbers.pop();
		step.clear();
		
		numbers.push(first);
		step[first] = 0;
		
		while(!cin.eof())
		{
			string tmp;
			
			if(numbers.front() == "01234567")
			{
				cout << step[numbers.front()] << endl;
				break;
			}
			
			for(int i = 0; i < 8; ++i)
			{	
				if(numbers.front()[i] == '0')
				{
					tmp = numbers.front();
					if(i > 3 && step.count(exchange(i, i-4, tmp)) == 0)
					{
						numbers.push(exchange(i, i-4, tmp));
						step[exchange(i, i-4, tmp)] = 1 + step[tmp];
					}
					if(i < 4 && step.count(exchange(i, i+4, tmp)) == 0)
					{
						numbers.push(exchange(i, i+4, tmp));
						step[exchange(i, i+4, tmp)] = 1 + step[tmp];
					}
					if(i != 0 && i != 4 && step.count(exchange(i, i-1, tmp)) == 0)
					{
						numbers.push(exchange(i, i-1, tmp));
						step[exchange(i, i-1, tmp)] = 1 + step[tmp];
					}
					if(i != 3 && i != 7 && step.count(exchange(i, i+1, tmp)) == 0)
					{
						numbers.push(exchange(i, i+1, tmp));
						step[exchange(i, i+1, tmp)] = 1 + step[tmp];
					}
					numbers.pop();
	}}}}
	
	
	return 0;
}