uses crt;
var
  aa,bb,s,f,e:longint;
  c,cc:char;
  t,r:array['A'..'F'] of longint;
  p1,d,p2:array[1..100,1..100,1..4] of longint;
  visit:array[0..100,0..100] of longint;
  maze:array[1..100,1..100] of char;
  b1,b2,b3:array[1..100000] of longint;
  procedure calc(a,b:longint);
  var
    x,y,z:longint;
  begin
     x:=a;
     y:=b;
     z:=0;
     while maze[x-1,y] <> '#' do begin
           x:=x-1;
           inc(z);
     end;
     d[a,b,1]:=z;
     p1[a,b,1]:=x;
     p2[a,b,1]:=y;
     x:=a;
     y:=b;
     z:=0;
     while maze[x+1,y] <> '#' do begin
           x:=x+1;
           inc(z);
     end;
     d[a,b,2]:=z;
     p1[a,b,2]:=x;
     p2[a,b,2]:=y;
     x:=a;
     y:=b;
     z:=0;
     while maze[x,y-1] <> '#' do begin
           y:=y-1;
           inc(z);
     end;
     d[a,b,3]:=z;
     p1[a,b,3]:=x;
     p2[a,b,3]:=y;

     x:=a;
     y:=b;
     z:=0;
     while maze[x,y+1] <> '#' do begin
           y:=y+1;
           inc(z);
     end;
     d[a,b,4]:=z;
     p1[a,b,4]:=x;
     p2[a,b,4]:=y;

  end;
begin
  assign(input,'DATA5.txt'); reset(input);
  assign(output,'OUT5.txt'); rewrite(output);
  for aa:= 0 to 11 do begin
      for bb:= 0 to 11 do begin
          maze[aa,bb]:='#';
          visit[aa,bb]:=1;
      end;
  end;
  for aa:= 1 to 10 do begin
      for bb:= 1 to 10 do begin
          read(maze[aa,bb]);
          if maze[aa,bb] <> '#' then begin
             visit[aa,bb]:=0;
          end;
          if maze[aa,bb] in ['A'..'F'] then begin
             t[maze[aa,bb]]:=aa;
             r[maze[aa,bb]]:=bb;
          end;
      end;
      readln;
  end;
  for aa:= 1 to 10 do begin
      for bb:= 1 to 10 do begin
          calc(aa,bb);
      end;
  end;
  for cc:= 'A' to 'F' do begin
  for aa:= 1 to 100 do begin
      for bb:= 1 to 100 do begin
         if maze[aa,bb] <> '#' then
          visit[aa,bb]:=0;
      end;
  end;
  b1[1]:=t[cc];
  b2[1]:=r[cc];
  b3[1]:=0;
  s:=1;
  f:=2;
  c:=cc;
  inc(c);
  e:=0;
  while s <> f do begin
        visit[b1[s],b2[s]]:=1;
        if maze[b1[s],b2[s]] = c then begin
           writeln(b3[s]-e);
           e:=e+b3[s];
           break;
        end;
        if visit[p1[b1[s],b2[s],1],p2[b1[s],b2[s],1]] = 0 then begin
           visit[p1[b1[s],b2[s],1],p2[b1[s],b2[s],1]]:=1;
           b1[f]:=p1[b1[s],b2[s],1];
           b2[f]:=p2[b1[s],b2[s],1];
           b3[f]:=b3[s]+d[b1[s],b2[s],1];
           inc(f);
        end;
        if visit[p1[b1[s],b2[s],2],p2[b1[s],b2[s],2]] = 0 then begin
           visit[p1[b1[s],b2[s],2],p2[b1[s],b2[s],2]]:=1;
           b1[f]:=p1[b1[s],b2[s],2];
           b2[f]:=p2[b1[s],b2[s],2];
           b3[f]:=b3[s]+d[b1[s],b2[s],2];
           inc(f);
        end;
        if visit[p1[b1[s],b2[s],3],p2[b1[s],b2[s],3]] = 0 then begin
           visit[p1[b1[s],b2[s],3],p2[b1[s],b2[s],3]]:=1;
           b1[f]:=p1[b1[s],b2[s],3];
           b2[f]:=p2[b1[s],b2[s],3];
           b3[f]:=b3[s]+d[b1[s],b2[s],3];
           inc(f);
        end;
        if visit[p1[b1[s],b2[s],4],p2[b1[s],b2[s],4]] = 0 then begin
           visit[p1[b1[s],b2[s],4],p2[b1[s],b2[s],4]]:=1;
           b1[f]:=p1[b1[s],b2[s],4];
           b2[f]:=p2[b1[s],b2[s],4];
           b3[f]:=b3[s]+d[b1[s],b2[s],4];
           inc(f);
        end;
        inc(s);
  end;
  end;
  close(input); close(output);
end.

