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

map<string,int> M ;

int move[4][2] = {1,0,-1,0,0,1,0,-1};
void get_map()
{
	string start = "01234567";
	M[start] = 0;
	queue<string> que ;
	que.push(start);
	
	while (!que.empty())
	{
		string f = que.front();
		que.pop();
		int zero = f.find_first_of('0');
		int x =zero/4;
		int y = zero%4;
		for (int i=0;i<4;i++)
		{
			int X = x+move[i][0];
			int Y = y+move[i][1];	
			if (X<0 || X>1|| Y <0|| Y>3) continue ;
			int newpos =X*4+Y;
			string item = f ;
			swap(item[zero],item[newpos]);
			if (M.find(item)!=M.end()) 
				continue ;
			else 
				{
					M[item] = M[f] + 1;
					que.push(item);
				}
		}
	}
} 

int main ()
{
	get_map();
	string s ;
	while (cin >> s)
	{
		string item;
		for (int i=0 ;i<7;i++)
		{
			cin >> item ;
			s += item;
		}
		cout <<M[s]<<endl;
	}
}
