program game07;
uses crt;

type TState=array[1..16] of byte;

const FS:TState=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var switch:byte;
    params:array[1..3] of string;
    BS:TState;
    fBS:text;
    fFS:text;
    Buf:char;
    ret,cis:string;
    c:integer;
    i,ii,iii:byte;




procedure Solve(Zacatek:TState);
const max=100;
type PStruct=^TStruct;
     TStruct=record
        is:boolean;
        kam:array[1..4] of PStruct;
       end;

var First:PStruct;
    Pozice:array[0..max] of TState;
    stopy:array[1..max] of byte; {dolu=1,nahoru=2,doleva=3,doprava=4}
    n:array[0..max] of byte;
    delka:byte; {soucasna delka stromu}
    zrovna:TState;
    h,hh:byte;

procedure Rekur(Actual:PStruct;ActKrok:byte);
var k:byte;
begin
 if ActKrok<Delka then {jestlize se jeste nenachazime na koneccich stromu}
 for k:=1 to 4 do  {tak sestoupime jest niz}
  if actual^.kam[k]^.is then  {pres toto se nikam dal nejde}
    begin
     case k of
      1:begin   {dolu}
           Pozice[actkrok+1]:=Pozice[actkrok];
           Pozice[ActKrok+1][n[ActKrok]]:=pozice[actkrok+1][n[actkrok]-4];
           n[actkrok+1]:=n[actkrok]-4;
           pozice[actkrok+1][n[actkrok+1]]:=16;
           stopy[actkrok]:=1;
          end;
      2:begin   {nahoru}
           Pozice[actkrok+1]:=Pozice[actkrok];
           Pozice[ActKrok+1][n[ActKrok]]:=pozice[actkrok+1][n[actkrok]+4];
           n[actkrok+1]:=n[actkrok]+4;
           pozice[actkrok+1][n[actkrok+1]]:=16;
           stopy[actkrok]:=2;
          end;
      3:begin {doleva}
           Pozice[actkrok+1]:=Pozice[actkrok];
           Pozice[ActKrok+1][n[ActKrok]]:=pozice[actkrok+1][n[actkrok]+1];
           n[actkrok+1]:=n[actkrok]+1;
           pozice[actkrok+1][n[actkrok+1]]:=16;
           stopy[actkrok]:=3;
          end;
      4:begin {doprava}
            Pozice[actkrok+1]:=Pozice[actkrok];
           Pozice[ActKrok+1][n[ActKrok]]:=pozice[actkrok+1][n[actkrok]-1];
           n[actkrok+1]:=n[actkrok]-1;
           pozice[actkrok+1][n[actkrok+1]]:=16;
           stopy[actkrok]:=4;
          end;
     end; {of case}
     Rekur(Actual^.kam[k],ActKrok+1);
    end;{of for}

 if ActKrok=Delka then {kdyz uz jsme na konci stromu}
  begin
   for k:=1 to 4 do {tak vyrobime nove konecky}
    begin
     case k of
      1:begin{dolu}
         New(Actual^.kam[k]);
         Actual^.kam[k]^.is:=true;
         zrovna:=Pozice[actkrok];
         if n[actkrok]>12 then Actual^.kam[k]^.is:=false else
         begin
          zrovna[n[actkrok]]:=zrovna[n[actkrok]-4];
          zrovna[n[actkrok]-4]:=16;
         end;
        end; {of dolu}
      2:begin {nahoru}
         New(Actual^.kam[k]);
         Actual^.kam[k]^.is:=true;
         zrovna:=Pozice[actkrok];
         if n[actkrok]<5 then Actual^.kam[k]^.is:=false else
         begin
          zrovna[n[actkrok]]:=zrovna[n[actkrok]+4];
          zrovna[n[actkrok]+4]:=16;
         end;
        end; {of nahoru}
      3:begin {doleva}
         New(Actual^.kam[k]);
         Actual^.kam[k]^.is:=true;
         zrovna:=Pozice[actkrok];
         if n[actkrok] mod 4 <>3 then Actual^.kam[k]^.is:=false else
         begin
          zrovna[n[actkrok]]:=zrovna[n[actkrok]+1];
          zrovna[n[actkrok]+1]:=16;
         end;
        end; {of doleva}
      4:begin {doprava}
         New(Actual^.kam[k]);
         Actual^.kam[k]^.is:=true;
         zrovna:=Pozice[actkrok];
         if n[actkrok] mod 4 <>1 then Actual^.kam[k]^.is:=false else
         begin
          zrovna[n[actkrok]]:=zrovna[n[actkrok]-1];
          zrovna[n[actkrok]-1]:=16;
         end;
        end; {of doprava}
      end; {of case}

      h:=0;
      while (h<=actkrok) and (actual^.kam[k]^.is) do
      begin
       hh:=0;
       repeat
        inc(hh);
       until (Pozice[h][hh]<>zrovna[hh]) and (hh>=17);
       if hh>=17 then actual^.kam[k]^.is:=false;
       inc(h);
      end;

      {nasli sem konecne reseni}
      repeat
       inc(hh);
      until (fs[hh]<>zrovna[hh]) and (hh>=17);
      if hh>=17 then begin
                     h:=1;
                     while h<=actkrok do
                      begin
                       write(Stopy[h]);
                       inc(h);
                      end;
                     exit
                    end;

    end;{of for}


  end;{of if krok=delka}

 end;{of rekur}

begin
 New(First);
 Pozice[0]:=zacatek;
 h:=0;
 repeat
  inc(h);
 until zacatek[h]=16;
 n[0]:=h;
 writeln(n[0]);
 readln;
 delka:=1;

 repeat
  Rekur(first,1);
  inc(delka);
 until keypressed;

end;

function Inverze(Stav:TState):boolean;{sudy-true,lichy-false}
var b,bb:byte;
    pocinv:word;
begin
 b:=0;
 repeat
 inc(b);
 until stav[b]=16;
 pocinv:=0;
 while b<12 do
         begin
          stav[b]:=stav[b+4];
          stav[b+4]:=16;
          b:=b+4
         end;
 while b mod 4 >0 do
        begin
         stav[b]:=stav[b+1];
         stav[b+1]:=16;
         b:=b+1;
        end;

 for b:=1 to 15 do
  for bb:=b+1 to 16 do
   if Stav[b]>Stav[bb] then inc(PocInv);
 if PocInv/2=PocInv div 2 then
  Inverze:=true else Inverze:=false;
end;


begin
 clrscr;
 for i:=1 to paramcount do
  params[i]:=paramstr(i);
 if params[1][1]='-' then
  if params[1]=
   '-solve' then switch:=2 else
   if params[1]='-show' then switch:=3
   else begin write('unknow parameter');exit;end
  else switch:=1;


  if switch=1 then
   assign(fBS,params[1]) else
   assign(fBS,params[2]);
  {$I-}
   reset(fBS);
   if IOResult<>0 then begin write('Wrong name of input file');exit;end;
  {$I+}

  iii:=0;
  repeat
   readln(fbs,ret);
   ii:=0;
   repeat
    repeat
     inc(ii);
    until (ret[ii]<>' ');
    cis:='';
    if ii<=length(ret) then
     repeat
      cis:=cis+ret[ii];
      inc(ii);
     until (ret[ii]=' ') or (ii>length(ret));
    begin inc(iii); val(cis,bs[iii],c);if bs[iii]=0 then bs[iii]:=16;end;
   until ii>length(ret);
  until eof(fbs);
  close(fbs);


  if inverze(bs)=inverze(fs) then
   begin
    if switch=1 then
     begin
      writeln('Je to resitelne');
      readln;
      exit;
     end;
    if switch=2 then
     begin
      solve(bs);
     end;

   end
    else writeln('Neni to resitelne');


end.