#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
using namespace std;

map<string,int> dp;
int x[4] = {1,-1,4,-4};  //分别对应右移，左移，下移，上移 

void bfs()
{
	string s = "01234567";
	dp["01234567"] = 0;
	queue<string> q;
	q.push(s);
	while(!q.empty())
	{
		string now = q.front();
		q.pop();
		int m,n;
		m = now.find('0');         //m表示0的位置 
		for(int i = 0;i < 4;i++)
		{
			n = m + x[i];
			if(n >= 0&&n <= 7&& !(m == 3&&i == 0) && !(m == 4&&i == 1) )    //0可以向这个方向移动 
			{
				string New = now;
				char temp = New.at(m);
				New.at(m) = New.at(n);
				New.at(n) = temp;
				if(dp.find(New) == dp.end())                                //尚未出现过这种状况 
				{
					dp[New] = dp[now] + 1;
					q.push(New); 
				}
			}
			
	    }
	}
} 

int main()
{
	bfs();
	string s;s.clear();
	char c;
	int l = 0;
	while(cin>>c)
	{
		if(c >= '0'&&c <= '9')
		{
		    s.push_back(c);
		    l++;
	    }
		if(l == 8)
		{
			cout<<dp[s]<<endl;
			s.clear();
			l = 0;
		}
	}
}
