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

const int N = 8;
int TARGET37[N] = {-1,-1,-1, 3,-1,-1,-1, 7};
int TARGET26[N] = {-1,-1, 2, 3,-1,-1, 6, 7};
int TARGET15[N] = {-1, 1, 2, 3,-1, 5, 6, 7};
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 = 5040;
int DIST[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;
}
void swap(int i,int j)
{
	int tmp = CACHE[i];
	CACHE[i] = CACHE[j];
	CACHE[j] = tmp;
}
////////////////////////////////////
// left rotate
void rorate1L()
{
	int tmp 	 = CANDIDATE[1];
	CANDIDATE[1] = CANDIDATE[5];
	CANDIDATE[5] = CANDIDATE[4];
	CANDIDATE[4] = tmp;
}
void rorate2L()
{
	int tmp 	 = CANDIDATE[1];
	CANDIDATE[1] = CANDIDATE[2];
	CANDIDATE[2] = CANDIDATE[6];
	CANDIDATE[6] = CANDIDATE[5];
	CANDIDATE[5] = CANDIDATE[4];
	CANDIDATE[4] = tmp;
}
void rorate3L()
{
	int tmp 	 = CANDIDATE[1];
	CANDIDATE[1] = CANDIDATE[2];
	CANDIDATE[2] = CANDIDATE[3];
	CANDIDATE[3] = CANDIDATE[7];
	CANDIDATE[7] = CANDIDATE[6];
	CANDIDATE[6] = CANDIDATE[5];
	CANDIDATE[5] = CANDIDATE[4];
	CANDIDATE[4] = tmp;
}
////////////////////////////////////
// right rotate
void rorate1R()
{
	int tmp 	 = CANDIDATE[4];
	CANDIDATE[4] = CANDIDATE[5];
	CANDIDATE[5] = CANDIDATE[1];
	CANDIDATE[1] = tmp;
}
void rorate2R()
{
	int tmp 	 = CANDIDATE[4];
	CANDIDATE[4] = CANDIDATE[5];
	CANDIDATE[5] = CANDIDATE[6];
	CANDIDATE[6] = CANDIDATE[2];
	CANDIDATE[2] = CANDIDATE[1];
	CANDIDATE[1] = tmp;
}
void rorate3R()
{
	int tmp 	 = CANDIDATE[4];
	CANDIDATE[4] = CANDIDATE[5];
	CANDIDATE[5] = CANDIDATE[6];
	CANDIDATE[6] = CANDIDATE[7];
	CANDIDATE[7] = CANDIDATE[3];
	CANDIDATE[3] = CANDIDATE[2];
	CANDIDATE[2] = CANDIDATE[1];
	CANDIDATE[1] = tmp;
}

////////////////////////////////////
// solve
int solve()
{
	int step=0;

//	for(int i=0;i<40;i++)
//	std::cout << '=';
//	std::cout << std::endl;

	// let CACHE[0]==0 and calculate step
	int zero_position=0;
	for(;zero_position<N;zero_position++)
		if(CACHE[zero_position]==0)	break;
	int col = zero_position%(N/2);
	int row = zero_position/(N/2);
	if(row==1)
	{
		step++;
		swap(col,col+4);
	}
	while(col!=0)
	{
		step++;
		swap(col-1,col);
		col--;
	}

	// reset distance
	for(int i=0;i<MAX_STATE;i++)
		DIST[i] = MAX_STATE;
	// init frontier
	std::vector<int> frontier(1,cache2num());
	DIST[frontier[0]] = step;

	int cand_num;
	while(frontier.size()!=0)
	{
		std::vector<int> next_frontier;
		for(auto n : frontier)
		{
			num2cache(n);

			int zero_position=0;
			for(;zero_position<N;zero_position++)
				if(CACHE[zero_position]==0)	break;

			col = zero_position%(N/2);
			row = zero_position/(N/2);
			// try 1 rotation
			cache2cand();
			rorate1R();
			cand_num = cand2num();
			if(DIST[n]+4<DIST[cand_num])
			{
				DIST[cand_num] = DIST[n]+4;
				if(cand_num!=0)
					next_frontier.push_back(cand_num);
			}
			cache2cand();
			rorate1L();
			cand_num = cand2num();
			if(DIST[n]+4<DIST[cand_num])
			{
				DIST[cand_num] = DIST[n]+4;
				if(cand_num!=0)
					next_frontier.push_back(cand_num);
			}

			// try 2 rotation
			cache2cand();
			rorate2R();
			cand_num = cand2num();
			if(DIST[n]+6<DIST[cand_num])
			{
				DIST[cand_num] = DIST[n]+6;
				if(cand_num!=0)
					next_frontier.push_back(cand_num);
			}
			cache2cand();
			rorate2L();
			cand_num = cand2num();
			if(DIST[n]+6<DIST[cand_num])
			{
				DIST[cand_num] = DIST[n]+6;
				if(cand_num!=0)
					next_frontier.push_back(cand_num);
			}

			// try 3 rotation
			cache2cand();
			rorate3R();
			cand_num = cand2num();
			if(DIST[n]+8<DIST[cand_num])
			{
				DIST[cand_num] = DIST[n]+8;
				if(cand_num!=0)
					next_frontier.push_back(cand_num);
			}
			cache2cand();
			rorate3L();
			cand_num = cand2num();
			if(DIST[n]+8<DIST[cand_num])
			{
				DIST[cand_num] = DIST[n]+8;
				if(cand_num!=0)
					next_frontier.push_back(cand_num);
			}
//			if(DIST[0]!=MAX_STATE) break;
		}
		frontier = next_frontier;
//		if(DIST[0]!=MAX_STATE) break;
	}
	return DIST[0];
}
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
*/