program Dwite3;
  uses crt;
  const l=10;
       w=19;
  var map: array[0..l+1,0..w+1] of char;
      letter: array[1..5] of string;
      stepmap: array [1..l,1..w] of integer;
      destination: char;
      move: integer;
      x,y: integer;
      i,j: integer;
      finish: boolean;
      xcoord, ycoord: integer;
      count: integer;
      n: integer;
      Data3, Out3:text;

  begin
    clrscr;
    {Read Input}
    assign (Data3, 'DATA3.txt');
    reset (Data3);
    assign (Out3, 'OUT3.txt');
    rewrite (Out3);
    for y:= 0 to l+1 do
      for x:= 0 to w+1 do
        map[y,x]:='#';
    for y:=1 to l do
      begin
        for x:=1 to w do
          begin
            read(Data3,map[y,x]);
          end;
        readln(Data3);
      end;
    for n:=1 to 5 do
      begin
        readln (Data3, letter[n]);
      end;

    {Main Engine}

    for i:= 1 to 5 do
      begin
        move:=0;
        destination:=letter[i,1];
        for y:=1 to l do
              for x:= 1 to w do
                begin
                  if map[y,x]= destination then
                    begin
                      xcoord:=x;
                      ycoord:=y;
                    end;
                end;

        for j:= 1 to length(letter[i]) do
           begin
             for y:=1 to l do
              for x:= 1 to w do
                begin
                  stepmap[y,x]:=-1;
                end;
             finish:=false;
             count:=0;
             destination:=letter[i,j];
             stepmap[ycoord,xcoord]:=0;
             repeat
               move:=move+1;
               count:= count+1;
               for y:=1 to l do
                 for x:=1 to w do
                  begin
                   if (stepmap[y,x]=count-1) then
                    begin
                     if (map[y,x]=destination) then
                      begin
                        move:=move-1;
                        finish:=true;
                      end
                     else
                      begin
                     if (map[y+1,x]<>'#')and (stepmap[y+1,x]=-1) then
                       begin
                         if map[y+1,x]=destination then
                           begin
                            xcoord:=x-1;
                            ycoord:=y;
                            finish:=true;
                           end
                         else
                           stepmap[y+1,x]:=count;
                       end;

                     if (map[y-1,x]<>'#')and (stepmap[y-1,x]=-1) then
                       begin
                         if map[y-1,x]=destination then
                           begin
                            xcoord:=x;
                            ycoord:=y-1;
                            finish:=true;
                           end
                         else
                           stepmap[y-1,x]:=count;
                       end;

                     if (map[y,x+1]<>'#')and (stepmap[y,x+1]=-1) then
                       begin
                         if map[y,x+1]=destination then
                           begin
                            xcoord:=x+1;
                            ycoord:=y;
                            finish:=true;
                           end
                         else
                           stepmap[y,x+1]:=count;
                       end;
                     if (map[y,x-1]<>'#')and (stepmap[y,x-1]=-1) then
                       begin
                         if map[y,x-1]=destination then
                           begin
                            xcoord:=x-1;
                            ycoord:=y;
                            finish:=true;
                           end
                         else
                           stepmap[y,x-1]:=count;
                       end;
                    end;
                  end;
                  end;
              until (finish=true);
          end;
        writeln (Out3,move);
    end;
    close (Out3);
  end.


































