#include<iostream>
#include<queue>
#include<string.h>
#include<string>
#include<map>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
string str;  int dx[4]={-1,1,4,-4};  int d[8][8][8][8][8][8][8][8];
int posi;
void bfs()
{
    queue <string> que;
    que.push(str);
    d[str[0]-'0'][str[1]-'0'][str[2]-'0'][str[3]-'0'][str[4]-'0'][str[5]-'0'][str[6]-'0'][str[7]-'0']=0;
    while(que.size())
    {
    	string preq=que.front(); que.pop();
    	if(preq=="01234567") 
    	{
    		cout<<d[0][1][2][3][4][5][6][7]<<endl; break;
		}
		for(int i=0;i<8;i++) if(preq[i]=='0') posi=i; 
		for(int i=0;i<4;i++)
		{
		    	int nposi=posi+dx[i];
		    	if(nposi>7||nposi<0) continue;
		    	if(i==0&&nposi==3||i==1&&nposi==4) continue;
				string q=preq;
				swap(q[posi],q[nposi]);
				if(d[q[0]-'0'][q[1]-'0'][q[2]-'0'][q[3]-'0'][q[4]-'0'][q[5]-'0'][q[6]-'0'][q[7]-'0']) continue;
				que.push(q);
				d[q[0]-'0'][q[1]-'0'][q[2]-'0'][q[3]-'0'][q[4]-'0'][q[5]-'0'][q[6]-'0'][q[7]-'0']=d[preq[0]-'0'][preq[1]-'0'][preq[2]-'0'][preq[3]-'0'][preq[4]-'0'][preq[5]-'0'][preq[6]-'0'][preq[7]-'0']+1;
		} 
	}
} 

int main()
{	
	while(true) 
	{
	   memset(d,0,sizeof(d));
	   int a;
	   str="";
	   for(int i=0;i<8;i++)
	   {
	       scanf("%d",&a);
		   str+=a+'0';	
	   } 
	   
	   bfs();
	}
	return 0;
}
