#include<iostream>
#include<queue>
#include<set>
#include<algorithm>

typedef struct data
{
	int puzzle[8];
	int pos;
	int num;
}data;

int convert(const int array[8]);
int bfs(data init);

int main()
{
	data init;
	
	while(1)
	{
		std::cin>>init.puzzle[0]>>init.puzzle[1]>>init.puzzle[2]>>init.puzzle[3]>>init.puzzle[4]>>init.puzzle[5]>>init.puzzle[6]>>init.puzzle[7];
		if(std::cin.eof())break;
		
		for(int i=0;i<8;i++)
		{
			if(init.puzzle[i]==0)
			{
				init.pos=i;
				break;
			}
		}
		init.num=0;
		std::cout<<bfs(init)<<std::endl;
	}
	return 0;
}

int bfs(data init)
{
	std::queue<data> que;
	std::set<int> visited;
	que.push(init);
	
	while(1)
	{
		bool finished=true;
		data now,next;

		now=que.front();
		for(int i=0;i<8;i++){if(now.puzzle[i]!=i)finished=false;}
		if(finished)break;
		que.pop();
		
		int temp=convert(now.puzzle);
		
		if(visited.find(temp)!=visited.end())continue;
		visited.insert(temp);
		
//		std::cout<<now.num<<' '<<temp<<std::endl;
		
		next.num=now.num+1;
		
		for(int i=0;i<8;i++){next.puzzle[i]=now.puzzle[i];}
		std::swap(next.puzzle[now.pos],next.puzzle[(now.pos+4)%8]);
		next.pos=(now.pos+4)%8;
		que.push(next);
		
		for(int i=0;i<8;i++){next.puzzle[i]=now.puzzle[i];}
		std::swap(next.puzzle[now.pos],next.puzzle[(now.pos+1)%4+(now.pos/4)*4]);
		next.pos=(now.pos+1)%4+(now.pos/4)*4;
		que.push(next);

		for(int i=0;i<8;i++){next.puzzle[i]=now.puzzle[i];}
		std::swap(next.puzzle[now.pos],next.puzzle[(now.pos+3)%4+(now.pos/4)*4]);
		next.pos=(now.pos+3)%4+(now.pos/4)*4;
		que.push(next);
	}
	return que.front().num;
}

int convert(const int array[8])
{
	int temp=0,power=1;
	for(int i=0;i<8;i++)
	{
		temp+=array[i]*power;
		power*=10;
	}
	return temp;
}