// 2012/03/02 Tazoe

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

int BFS(string S){
	queue<string> Q;
	map<string, int> D;

	Q.push(S);
	D[S] = 0;

	while(Q.size()){
		string q = Q.front();
		Q.pop();

		if(q=="01234567")
			return D[q];

		int I;
		for(I=0; I<8; I++){
			if(q[I]=='0')
				break;
		}

		string r = q;
		if(I>=0&&I<=3){
			r[I] = r[I+4];
			r[I+4] = '0';
		}
		else{
			r[I] = r[I-4];
			r[I-4] = '0';
		}
		if(D.find(r)==D.end()){
			Q.push(r);
			D[r] = D[q]+1;
		}

		r = q;
		if(I!=0&&I!=4){
			r[I] = r[I-1];
			r[I-1] = '0';
		}
		if(D.find(r)==D.end()){
			Q.push(r);
			D[r] = D[q]+1;
		}

		r = q;
		if(I!=3&&I!=7){
			r[I] = r[I+1];
			r[I+1] = '0';
		}
		if(D.find(r)==D.end()){
			Q.push(r);
			D[r] = D[q]+1;
		}
	}

	return -1;
}

int main()
{
	while(true){
		string S;
		cin >> S;
		if(cin.eof())
			break;

		for(int i=0; i<7; i++){
			string tmp;
			cin >> tmp;
			S += tmp;
		}

		int res = BFS(S);
		cout << res << endl;
	}

	return 0;
}