#include <cstdio>
#include <vector>
#include <iostream>

const int N = 8;
const int COL = 4;
const int ROW = 2;
const int TARGET_NUM = 0;
int CACHE[N] = {0,1,2,3,4,5,6,7};
int CANDIDATE[N] = {0,1,2,3,4,5,6,7};
const int FACTORIAL[N] = {1,1,2,6, 24,120,720,5040};

const int MAX_STATE = 400320;
bool IS_VISIT[MAX_STATE];

int cache2num()
{
	int result=0;
	bool is_available[N] = {true,true,true,true,true,true,true,true,};
	for(int i=0;i<N;i++)
	{
		int cp=0;
		for(int j=0;j<CACHE[i];j++)
			if(is_available[j])
				cp+=1;
		result += cp*FACTORIAL[N-1-i];
		is_available[CACHE[i]]=false;
	}
	return result;
}
int cand2num()
{
	int result=0;
	bool is_available[N] = {true,true,true,true,true,true,true,true,};
	for(int i=0;i<N;i++)
	{
		int cp=0;
		for(int j=0;j<CANDIDATE[i];j++)
			if(is_available[j])		cp+=1;
		result += cp*FACTORIAL[N-1-i];
		is_available[CANDIDATE[i]]=false;
	}
	return result;
}
void cache2cand()
{
	for(int i=0;i<N;i++)
		CANDIDATE[i]=CACHE[i];
}
void num2cache(int num)
{
	bool is_available[N] = {true,true,true,true,true,true,true,true};
	for(int i=0;i<N;i++)
	{
		int order = num/FACTORIAL[N-1-i];
		num = num % FACTORIAL[N-1-i];
		int index=0;
		while(!is_available[index])
			index++;
		for(int cp=0;cp<order;index++)
			if(is_available[index+1])
				cp++;
		CACHE[i] = index;
		is_available[index]=false;
	}
}
void print_cache()
{
	std::cout << "cache:";
	for(int i=0;i<N;i++)
		std::cout << CACHE[i] << ",";
	std::cout << std::endl;
}
void print_candidate()
{
	std::cout << "cand: ";
	for(int i=0;i<N;i++)
		std::cout << CANDIDATE[i] << ",";
	std::cout << std::endl;
}
int solve()
{
	for(int i=0;i<MAX_STATE;i++)
		IS_VISIT[i]=false;
	std::vector<int> frontier(1,cache2num());
//	print_cache();
//	std::cout << "---------------------------------" << std::endl;
	for(int step=0;step<MAX_STATE;step++)
	{
		std::vector<int> next_frontier;
		for(auto n : frontier)
		{
			if(n==TARGET_NUM) return step;

			num2cache(n);
//		std::cout << n << "\t";
//		print_cache();
/*
			for(int i=0;i<N;i++)
				for(int j=0;j<N;j++)
					if(i!=j && CACHE[i]==CACHE[j])
						exit(1);
*/
//			if(tmp==5)
//				return 0;
			int zero_position=0;
			for(;zero_position<N;zero_position++)
				if(CACHE[zero_position]==0)	break;

//			int row = zero_position/ROW;
			int col = zero_position%COL;

			cache2cand();
			int swap = CANDIDATE[col];
			CANDIDATE[col] = CANDIDATE[col+4];
			CANDIDATE[col+4] = swap;
			int cand_num = cand2num();
//		std::cout << cand_num << "\t";
//		print_candidate();
			if(!IS_VISIT[cand_num])
			{
				IS_VISIT[cand_num]=true;
				next_frontier.push_back(cand_num);
			}

			if(0<=col && col<=2)
			{
				cache2cand();
				int swap = CANDIDATE[zero_position];
				CANDIDATE[zero_position] = CANDIDATE[zero_position+1];
				CANDIDATE[zero_position+1] = swap;
				int cand_num = cand2num();
//			std::cout << cand_num << "\t";
//			print_candidate();
				if(!IS_VISIT[cand_num])
				{
					IS_VISIT[cand_num]=true;
					next_frontier.push_back(cand_num);
				}
			}

			if(1<=col && col<=3)
			{
				cache2cand();
				int swap = CANDIDATE[zero_position];
				CANDIDATE[zero_position] = CANDIDATE[zero_position-1];
				CANDIDATE[zero_position-1] = swap;
				int cand_num = cand2num();
//			std::cout << cand_num << "\t";
//			print_candidate();
				if(!IS_VISIT[cand_num])
				{
					IS_VISIT[cand_num]=true;
					next_frontier.push_back(cand_num);
				}
			}
		}
		frontier = next_frontier;
	}
	return -1;
}
int main()
{
	char cline[16];
	std::cin.getline(cline, sizeof(cline));
	while(0<=cline[0]-'0' && cline[0]-'0'<8)
	{
		int cp = 0;
		for(int i=0;i<16;i++)
			if(isdigit(cline[i]))
				CACHE[cp++]=cline[i]-'0';
		std::cout << solve() << std::endl;
		std::cin.getline(cline, sizeof(cline));
	}
	return 0;
}



/*

0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0


0
1
28
*/