#include "iostream"
#include "cstdio"
#include "string"
#include "queue"
#include "map"
using namespace std; 

map<string,int>pb;
map<string,int>vis;

int dx[4]={1,-1,4,-4};

void bfs()
{
	queue<string>qs;
	queue<int>qt;
	qs.push("01234567");
	qt.push(0);
	
	while(!qt.empty())
	{
		string ns=qs.front();qs.pop();
		int nt=qt.front();qt.pop();
		vis[ns]=1;
		
		int p;
		for(int i=0;i<8;i++)
			if(ns[i]=='0')
				p=i;
		
		for(int i=0;i<4;i++)
		{
			string ps;
			if(p+dx[i]>=0 && p+dx[i]<8)
			{
				ps=ns;
				swap(ps[p],ps[p+dx[i]]);
				if(!vis[ps])
				{
					//cout<<"#"<<ps<<" "<<nt+1<<endl;
					pb[ps]=nt+1;
					qs.push(ps);
					qt.push(nt+1);
				}
				
			}
			
			
		}
	}
}

int main()
{
	//freopen("e.txt","r",stdin);
	//freopen("e.out","w",stdout);
	string a;
	char t;
	ios::sync_with_stdio(false);
	bfs();
	while(cin>>t)
	{
		a+=t;
		for(int i=0;i<7;i++)
		{
			cin>>t;
			a+=t;
		}
		//if(a=="01234567")
			//cout<<0<<endl;
		//else
		cout<<pb[a]<<endl;
		a="";
	}
	return 0;
}
