#include <cstdio>
#include <set>
#include <vector>
#include <ctype.h>
#include <iostream>

bool is_find(std::vector<int> pieace,std::vector<int> cadidate)
{
	for(int i=0;i<8;i++)
		if(pieace[i]!=cadidate[i]) return false;
	return true;
}
int solve(std::vector<int> init_pieace)
{
	int total_step = 0;
	std::vector<int> target_list = {0,1,2,3,4,5,6,7};

	std::set<std::vector<int>> frontier;
	frontier.insert(init_pieace);

	std::set<std::vector<int>> previous_frontier;
	int step = 0;
	while(true)
	{
		std::set<std::vector<int>> next_frontier;
//		std::cout << "step1:"<< step << "\t" << frontier.size() << "\t" << next_frontier.size() << "\t" <<  previous_frontier.size() << std::endl;
		for(auto p : frontier)
		{
/*
			for(auto x : p)
				std::cout << x << ',';
			std::cout << std::endl;
*/
			if(is_find(target_list,p)) return step;
			int zero_position=0;
			for(int i=0;i<8;i++)
			{
				if(p[i]==0)
				{
					zero_position = i;
					break;
				}
			}
//			std::cout << "zero:" << zero_position << std::endl;
			int relative = zero_position%4;

			std::vector<int> new_peiace=p;
			int swap = new_peiace[relative];
			new_peiace[relative] = new_peiace[relative+4];
			new_peiace[relative+4] = swap;
//			if(previous_frontier.count(new_peiace)==0)
			if(std::find(previous_frontier.begin(), previous_frontier.end(), new_peiace) == previous_frontier.end())
				next_frontier.insert(new_peiace);

			if(0<=relative && relative<3)
			{
				std::vector<int> new_peiace=p;
				int swap = new_peiace[zero_position];
				new_peiace[zero_position] = new_peiace[zero_position+1];
				new_peiace[zero_position+1] = swap;
//				if(previous_frontier.count(new_peiace)==0)
				if(std::find(previous_frontier.begin(), previous_frontier.end(), new_peiace) == previous_frontier.end())
					next_frontier.insert(new_peiace);
			}

			if(0<relative && relative<=3)
			{
				std::vector<int> new_peiace=p;
				int swap = new_peiace[zero_position];
				new_peiace[zero_position] = new_peiace[zero_position-1];
				new_peiace[zero_position-1] = swap;
//				if(previous_frontier.count(new_peiace)==0)
				if(std::find(previous_frontier.begin(), previous_frontier.end(), new_peiace) == previous_frontier.end())
					next_frontier.insert(new_peiace);
			}
		}
/*
		if(next_frontier.size()==0)
		{
			std::cout << "somwthing wrong" << std::endl;
			exit(1);
		}
        std::cout << "------------------------------------------------------------------------------" << std::endl;
		for(auto p : next_frontier)
		{
			for(auto x : p)
				std::cout << x << ',';
			std::cout << std::endl;
		}
		std::cout << "==============================================================================" << std::endl;
		std::cout << "step2:"<< step << "\t" << frontier.size() << "\t" << next_frontier.size() << "\t" <<  previous_frontier.size() << std::endl;
*/
		previous_frontier = frontier;
		frontier = next_frontier;
		step++;
	}
}
int main()
{
	char cline[16];
	std::cin.getline(cline, sizeof(cline));
	while(0<=cline[0]-'0' && cline[0]-'0'<8)
	{
		std::vector<int> pieace(8);
		int cp = 0;
		for(int i=0;i<16;i++)
			if(isdigit(cline[i]))
				pieace[cp++]=cline[i]-'0';
		std::cout << solve(pieace) << std::endl;
		std::cin.getline(cline, sizeof(cline));
	}
}