#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<string>
#include<utility>
using namespace std;
typedef pair<string,int> P;
const string S("01234567");
char arr[9];
map<string,int> obj;
inline string swapno(string s,int i,int j)
{
	char mid;
	string re(s);
	mid=re[i];
	re[i]=re[j];
	re[j]=mid;
	return re;
}
void bfs()
{
	queue<P> que;
	string re;
	int num=1,n;
	que.push(make_pair(S,0));
	obj.insert(make_pair(S,0));
	P mid;
	while(!que.empty())
	{
		mid=que.front();
		que.pop();
		n=mid.first.find("0");
		if(n-1>3||(n-1>=0&&n<=3))
		{
			re=swapno(mid.first,n,n-1);
			if(!obj.count(re))
			{
				que.push(P(re,mid.second+1));
				obj.insert(P(re,mid.second+1));
			}
		}
		if(n+1<=3||(n+1<=7&&n>=4))
		{
			re=swapno(mid.first,n,n+1);			
			if(!obj.count(re))
			{
				que.push(P(re,mid.second+1));
				obj.insert(P(re,mid.second+1));
			}
		}
		
		if(n+4<=7)
		{
			re=swapno(mid.first,n,n+4);
			if(!obj.count(re))
			{
				que.push(P(re,mid.second+1));
				obj.insert(P(re,mid.second+1));
			}
		}
		if(n-4>=0)
		{	
			re=swapno(mid.first,n,n-4);
			if(!obj.count(re))
			{
				que.push(P(re,mid.second+1));
				obj.insert(P(re,mid.second+1));
			}
		}
	}
}
int main()
{
	int i;
	bfs();
	while(scanf("%c %c %c %c %c %c %c %c",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5],&arr[6],&arr[7])!=EOF)
	{
		getchar();
		int num;
		string no1=arr;
		printf("%d\n",obj[no1]);
	}
	return 0;
} 
