using namespace std;
int len[max];
int vis[max];
int s[9]={1,10,100,1000,10000,100000,1000000,10000000,100000000};
int jie[9]={0,1,2,6,24,120,720,5040,40320}; //n!
int four[4];
int start=123456789,str[9];
int temp;
int transform1(int num)
{
int result;
result = num%s[4];
result += ((num / s[5]) % 10)*s[4];
result += num / s[8] * s[5];
result += ((num / s[6]) % 10)*s[6];
result += ((num / s[4]) % 10)*s[7];
result += ((num / s[7]) % 10)*s[8];
return result;
}
int transform2(int num)
{
int result = num / s[5] * s[5];
result += num / s[1] % 10;
result += num / s[4] % 10 * s[1];
result += num / s[2] % 10 * s[2];
result += num % 10 * s[3];
result += num / s[3] % 10 * s[4];
return result;
}
int transform3(int num)
{
int result = num / s[6] * s[6] + num % 10;
result += num / s[2] % 10 * s[1];
result += num / s[5] % 10 * s[2];
result += num / s[3] % 10 * s[3];
result += num / s[1] % 10 * s[4];
result += num / s[4] % 10 * s[5];
return result;
}
int transform4(int num)
{
int result = num % s[3] + num / s[8] * s[8];
result += num / s[4] % 10 * s[3];
result += num / s[7] % 10 * s[4];
result += num / s[5] % 10 * s[5];
result += num / s[3] % 10 * s[6];
result += num / s[6] % 10 * s[7];
return result;
}
int cantor(int key)
{
int result, temp[9];
for (int i = 8; i >= 0; i--)
{
temp[i] = key % 10;
key = key / 10;
}
result = 0;
for (int i = 0; i<8; i++)
{
int tot = 0;
for (int j = i + 1; j<9; j++)
if (temp[j]<temp[i])
tot++;
result += tot*jie[8 - i];
}
return result;
}
int main()
{
queue<int>que;
int key1,key2[4],top;
len[0]=0;
memset(vis,0,sizeof(vis));
vis[0]=1;
que.push(start);
while(que.size())
{
top=que.front();
que.pop();
key1=cantor(top);
four[0]=transform1(top);
four[1]=transform2(top);
four[2]=transform3(top);
four[3]=transform4(top);
for(int i=0;i<4;i++)
{
key2[i]=cantor(four[i]);
if(!vis[key2[i]])
{
vis[key2[i]]=1;
que.push(four[i]);
len[key2[i]]=len[key1]+1;
}
}
}
while(~scanf("%d",&str[0]))
{
temp=str[0];
for(int i=1;i<9;i++)
{
scanf("%d",&str[i]);
temp=str[i]+temp*10;
}
printf("%d\n", len[cantor(temp)]);
}
return 0;
}