#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
using namespace std;

map<string,int> table;
const int dir[4] = {1,-1,4,-4};
void bfs()
{
	queue<string> q;
	q.push("01234567");
	table["01234567"] = 0;
	while(!q.empty())
	{
		string now = q.front();q.pop();
		int j;
		for(j = 0;j < 8;++j)
		{
			if(now[j] == '0')
				break;
		}
		
		for(int i = 0;i < 4;++i)
		{
			int n = j + dir[i];
			if(n >= 0 && n < 8 && !(j == 3 && i == 0) && !(j == 4 && i == 1))
			{
				string next = now;
				swap(next[j],next[n]);
				if(!table.count(next))
					table[next] = table[now] + 1,q.push(next);
			}
		}
	}
}
int main()
{
	bfs();
	string line;
	while(getline(cin,line))
	{
		line.erase(remove(line.begin(),line.end(),' '),line.end());
		cout << table[line] << endl;
	}
	return 0;
}
