#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
class FC{
	public:
		int p[8];
		int zero;
		int n;
		bool is_over(){
			return p[0]==0 && p[1]==1 && p[2]==2 && p[3]==3 && p[4]==4 && p[5]==5 && p[6]==6 && p[7]==7; 
		}
		void rot1(){
			swap(p[zero],p[(zero+3+7)%(zero)]); 
		}
		void rot2(){
			swap(p[zero],p[(zero+1+7)%(zero)]);
		}
		void rot3(){
			swap(p[zero],p[(zero-1+7)%(zero)]);
		}
		
};
int main(){
		FC now;
		for(int j=0;j<8;j++)
			cin>>now.p[j];
		for(int j=0;j<8;j++)
			if(now.p[j]==0)
				now.zero=j;
		now.n=0;
		queue<FC> que;
		que.push(now);
		while(!que.empty()){
			now=que.front();
			que.pop();
		
			if(now.is_over()){
				cout<<now.n<<endl;
				break;
			}
			FC nex1=now; nex1.rot1(); nex1.n++; que.push(nex1);
			FC nex2=now; nex2.rot2(); nex2.n++; que.push(nex2);
			FC nex3=now; nex3.rot3(); nex3.n++; que.push(nex3);
			
		
	}
	return 0;
}