#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
int date[8][4]={
	{2,1,4,4},
	{3,0,2,5},
	{3,1,3,6},
	{2,2,7,7},
	{2,0,5,5},
	{3,1,4,6},
	{3,2,5,7},
	{2,3,6,6}
};
int main(){
	map<string,int>time;
	queue<string>name;
	time["01234567"]=0;
	name.push("01234567");
	string str,STR;
	while(name.empty()==0){
		str=name.front();
		int pos=0;
		for(int i=0;i<str.size();i++)if(str[i]=='0')pos=i;
		for(int i=1;i<=date[pos][0];i++){
			STR=str;
			swap(STR[pos],STR[date[pos][i]]);
			if(time[STR]==0&&STR!="01234567"){
				time[STR]=time[str]+1;
				name.push(STR);
			}
		}
		name.pop();
	}
	while(getline(cin,STR)){
		str="";
		for(int i=0;i<STR.size();i+=2)str+=STR[i];
		cout<<time[str]<<endl;
	}
	
	return 0;
}
