program p70;
const
zs=1200007;
d:array[1..4]of integer=(-1,1,-3,3);
type
rc1=record
ss:string[9];
fl:boolean;
end;
rc2=record
st:string[9];
sp:longint;
end;
var v:array[0..1300000]of rc1;
q:array[0..1300000]of rc2;
cl,op,i,n,m,t:longint; s:string[9];
function hash(x:string):longint;
var y:longint; code:integer;
begin
val(x,y,code);
hash:=y mod zs;
end;
procedure makehash(x:string);
var h:longint;
begin
h:=hash(x);
while v[h].fl=true do
h:=(h+1) mod zs;
v[h].fl:=true;
v[h].ss:=x;
end;
function judge(x:string):boolean;
var h:longint;
begin
h:=hash(x);
if v[h].fl=false then
exit(false) else
begin
while (v[h].fl=true)and(v[h].ss<>x) do
h:=(h+1) mod zs;
if v[h].fl=false then
exit(false) else
exit(true);
end;
end;
begin
readln(s);
cl:=1; op:=0;
q[cl].st:=s;
makehash(s);
while op<cl do
begin
inc(op);
for i:=1 to 4 do
begin
s:=q[op].st;
m:=pos(’0’,s);
t:=m+d[i];
if ((m in [1,2,5,8,9])
and((t>=1)and(t<=9)))
or ((m=4)and(t in [1,5,7]))
or ((m=6)and(t in [2,5,9]))
or((m=3)and(t in [2,6]))
or((m=7)and(t in [4,8]))then
begin
s[m]:=s[t];
s[t]:=’0’;
if q[op].st=’123807465’ then
begin
writeln(q[op].sp);
exit;
end;
if judge(s)=false then
begin
inc(cl);
q[cl].st:=s;
q[cl].sp:=q[op].sp+1;
makehash(s);
end;
end;
end;
end;
end.