long f[362881],d[362881],c[10];
long turn (char a[10])
{
long m=0,i,j,k,n;
for (i=0;i<=8;i++)
{k=0;
for (j=i+1,k=0;j<=8;j++)
if (a[i]>a[j]) k++;
for(j=1,n=1;j<=8-i;j++) n*=j;
m+=n*k;
}
return m;
}
void turnback (long m)
{
int i,j,k,l;
for (i=0;i<=8;i++)
c[i]='*';
for (i=8;i>=0;i++)
{
for (k=1,j=1;j<=i;j++) k*=j;
l=m/k;
for (k=8;l>=0;k--) if (c[k]=='*') l--;
c[k]=i+'0';
}
}
int where(char a[10])
{
int i;
for (i=0;i<=8;i++) if (a[i]=='0') return i;
}
int main()
{
long closed,open,m[10];
char a[10],now[10],now2[10],zero,t;
m[10]=turn("123804765");
scanf("%s",a);
d[0]=turn(a);
closed=-1;
open=1;
do
{
turnback(d[++closed]);
if (!f[turn©])
{f[turn(now)]=1;
zero=where©;
if (zero-3>=0) {now2=c;t=now2[zero-3];now2[zero-3]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
if (zero+3<=8) {now2=c;t=now2[zero+3];now2[zero+3]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
if (zero+1<=8) {now2=c;t=now2[zero+1];now2[zero+1]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
if (zero-1>=0) {now2=c;t=now2[zero-1];now2[zero-1]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
}
}
while(closed<open);
return 0;
}
long turn (char a[10])
{
long m=0,i,j,k,n;
for (i=0;i<=8;i++)
{k=0;
for (j=i+1,k=0;j<=8;j++)
if (a[i]>a[j]) k++;
for(j=1,n=1;j<=8-i;j++) n*=j;
m+=n*k;
}
return m;
}
void turnback (long m)
{
int i,j,k,l;
for (i=0;i<=8;i++)
c[i]='*';
for (i=8;i>=0;i++)
{
for (k=1,j=1;j<=i;j++) k*=j;
l=m/k;
for (k=8;l>=0;k--) if (c[k]=='*') l--;
c[k]=i+'0';
}
}
int where(char a[10])
{
int i;
for (i=0;i<=8;i++) if (a[i]=='0') return i;
}
int main()
{
long closed,open,m[10];
char a[10],now[10],now2[10],zero,t;
m[10]=turn("123804765");
scanf("%s",a);
d[0]=turn(a);
closed=-1;
open=1;
do
{
turnback(d[++closed]);
if (!f[turn©])
{f[turn(now)]=1;
zero=where©;
if (zero-3>=0) {now2=c;t=now2[zero-3];now2[zero-3]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
if (zero+3<=8) {now2=c;t=now2[zero+3];now2[zero+3]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
if (zero+1<=8) {now2=c;t=now2[zero+1];now2[zero+1]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
if (zero-1>=0) {now2=c;t=now2[zero-1];now2[zero-1]=now2[zero];now2[zero]=t;d[open++]=turn(now2);}
}
}
while(closed<open);
return 0;
}