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

using namespace std ;
map<string , int> m ;
void solve()
{
	m["01234567"] = 1 ;
	queue<string> q ;
	q.push("01234567") ;
	
	while (!q.empty())
	{
		int p ;
		string s= q.front(); q.pop();
		for (int i=0 ; i<8;i++)
			if (s[i]=='0')
			{
				p = i ;
				 break ;
			}
			
		
		if (p>=4)
		{
			string s2 = s ;
			swap(s2[p],s2[p-4]);
			if (m[s2] == 0)
			{
				m[s2] = m[s]+1;
		    	q.push(s2);
			}
			
		}
		
		if (p<4)
		{
			string s2 = s ;
			swap(s2[p],s2[p+4]);
			if (m[s2] == 0)
			{
				m[s2] = m[s]+1;
				q.push(s2);
			}

		}
		
		if (p!=0 && p!=4)
		{
			string s2 = s ;
			swap(s2[p],s2[p-1]);
			if (m[s2] == 0)
			{
				m[s2] = m[s]+1;
				q.push(s2);
			}
		}
		
		if (p!=3 && p!=7)
		{
			string s2 = s ;
			swap(s2[p],s2[p+1]);
			if (m[s2] == 0)
			{
				m[s2] = m[s]+1;
			q.push(s2);
			}
		}
		
	}
	
}

int main ()
{
	solve();
	int n;
	while (cin >> n)
	{
		string s ;
		s +='0'+n;
		for (int i=0;i<7;i++)
		{
			cin >> n;
			s +='0'+n;
		}
		cout <<m[s]-1<<endl;
	}
	
	
	return 0 ;
}
