#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int INF = 100000000;
bool vst[100000000] = {};

class Point{
public:
	int x;
	int y;
};

int main(){
	while(true){
		for(int i=0;i<INF;i++){
			vst[i] = 0;
		}
		int xxx;
		cin >> xxx;
		if(cin.eof())break;
		vector<int> S(10);
		for(int i=0;i<8;i++){
			if(i==0){
				S[0] = xxx;
			}else{
				cin >> S[i];
			}
			if(S[i]==0){
				S[8] = i;
			}
		}
		
		S[9] = 0;

		queue<vector<int> > que;
		que.push(S);

		while(!que.empty()){
			vector<int> N(que.front()); que.pop(); 

			if(vst[N[0]*10000000+N[1]*1000000+N[2]*100000+N[3]*10000+
				N[4]*1000+N[5]*100+N[6]*10+N[7]]==true){
				continue;
			}else{
				vst[N[0]*10000000+N[1]*1000000+N[2]*100000+N[3]*10000+
					N[4]*1000+N[5]*100+N[6]*10+N[7]]=true;
			}
		
		 	bool end = false;
			for(int i=0;i<9;i++){
				if(i!=N[i]&&i!=8){
					break;
				}
				if(i==8){
					cout << N[9] << endl;
					end = true;
				}
			}

			if(end)break;

			if(N[8]==0){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[0] = M[1];M[1] = 0;
					M[8] = 1;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[0] = L[4];L[4] = 0;
					L[8] = 4;L[9]++;
					que.push(L);

			}else if(N[8]==1){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[1] = M[0];M[0] = 0;
					M[8] = 0;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[1] = L[2];L[2] = 0;
					L[8] = 2;L[9]++;
					que.push(L);

					vector<int> O(10);
					for(int i=0;i<10;i++){
						O[i] = N[i];
					}
					O[1] = O[5];O[5] = 0;
					O[8] = 5;O[9]++;
					que.push(O);

			}else if(N[8]==2){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[2] = M[1];M[1] = 0;
					M[8] = 1;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[2] = L[3];L[3] = 0;
					L[8] = 3;L[9]++;
					que.push(L);

					vector<int> O(10);
					for(int i=0;i<10;i++){
						O[i] = N[i];
					}
					O[2] = O[6];O[6] = 0;
					O[8] = 6;O[9]++;
					que.push(O);

			}else if(N[8]==3){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[3] = M[2];M[2] = 0;
					M[8] = 2;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[3] = L[7];L[7] = 0;
					L[8] = 7;L[9]++;
					que.push(L);

			}else if(N[8]==4){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[4] = M[0];M[0] = 0;
					M[8] = 0;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[4] = L[5];L[5] = 0;
					L[8] = 5;L[9]++;
					que.push(L);

			}else if(N[8]==5){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[5] = M[1];M[1] = 0;
					M[8] = 1;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[5] = L[4];L[4] = 0;
					L[8] = 4;L[9]++;
					que.push(L);

					vector<int> O(10);
					for(int i=0;i<10;i++){
						O[i] = N[i];
					}
					O[5] = O[6];O[6] = 0;
					O[8] = 6;O[9]++;
					que.push(O);

			}else if(N[8]==6){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[6] = M[2];M[2] = 0;
					M[8] = 2;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[6] = L[5];L[5] = 0;
					L[8] = 5;L[9]++;
					que.push(L);

					vector<int> O(10);
					for(int i=0;i<10;i++){
						O[i] = N[i];
					}
					O[6] = O[7];O[7] = 0;
					O[8] = 7;O[9]++;
					que.push(O);

			}else if(N[8]==7){
					vector<int> M(10);
					for(int i=0;i<10;i++){
						M[i] = N[i];
					}
					M[7] = M[3];M[3] = 0;
					M[8] = 3;M[9]++;
					que.push(M);

					vector<int> L(10);
					for(int i=0;i<10;i++){
						L[i] = N[i];
					}
					L[7] = L[6];L[6] = 0;
					L[8] = 6;L[9]++;
					que.push(L);
			}
		}
	}

	return 0;
}
