program icp_final;
uses crt,dos;
var a:array[1..4,1..4] of byte;
    bx,by:array[1..4,1..4] of integer;
    tx1,ty1,tx,ty,k,l,chk_sum,tmp,i,j:integer;
    f1,f2:text;
    tm,fs1,fs2,fs3:string;
    c:string[3];
label 10;
{Procedures and Functions}
{==================================================================}{=======}
function y_pos(a:integer):integer;
  begin
   case a of
    1..4:y_pos:=1;
    5..8:y_pos:=2;
    9..12:y_pos:=3;
    13..16:y_pos:=4;
    end;
  end;
{==================================================================}{=======}
function x_pos(a:integer):integer;
  begin
   x_pos:=a-(y_pos(a)-1)*4;
  end;
{==================================================================}{=========}
procedure strelki;
var x,y:integer;
begin
 for x:=1 to 4 do
  for y:=1 to 4 do
   begin
    if x-x_pos(a[x,y])=0 then bx[x,y]:=0
      else
      begin
       if x-x_pos(a[x,y])<0 then bx[x,y]:=-1
         else
          bx[x,y]:=1;
      end;
   end;
  for x:=1 to 4 do
  for y:=1 to 4 do
   begin
    if y-y_pos(a[x,y])=0 then by[x,y]:=0
      else
      begin
       if y-y_pos(a[x,y])<0 then by[x,y]:=1
         else
          by[x,y]:=-1;
      end;
   end;
end;
{==================================================================}{======}
procedure show;
begin
   for j:=1 to 4 do
  begin
   for i:=1 to 4 do
    write(a[i,j],' ');
    writeln;
  end;
    for j:=1 to 4 do
  begin
   for i:=1 to 4 do
    write(bx[i,j]:2,by[i,j]:2,'  ');
    writeln;
  end;
  readkey;
end;
{==================================================================}{======}
function check:boolean;
var x,y:integer;
    aa:boolean;
begin
 aa:=true;
 for x:=1 to 4 do
  for y:=1 to 4 do
   if not((bx[x,y]=0) and (by[x,y]=0)) then aa:=false;
  check:=aa;
{  if aa=true then writeln('dima');}
end;
{==================================================================}
procedure move(x1,y1,x,y:integer);
var s:integer;
begin
 s:=a[x,y];
 a[x,y]:=a[x1,y1];
 a[x1,y1]:=s;
 tx:=x;
 ty:=y;
 tx1:=x1;
 ty1:=y1;
 strelki;
end;
procedure move_p(x,y:integer);
var lm,rm,um,dm:boolean;
begin
 if not check then
 begin
 move(tx,ty,x,y);
 show;
 lm:=false;rm:=false;um:=false;dm:=false;
 if x=1 then lm:=true;
 if x=4 then rm:=true;
 if y=1 then um:=true;
 if y=4 then dm:=true;
 if not(check)and(not(lm)and(bx[x-1,y]=-1)) then move_P(x-1,y);
 move(tx,ty,tx1,ty1);
 if not(check)and(not(rm)and(bx[x+1,y]=1))  then move_P(x+1,y);
 move(tx,ty,tx1,ty1);
 if not(check)and(not(dm)and(bx[x,y-1]=-1)) then move_P(x,y+1);
 move(tx,ty,tx1,ty1);
 if not(check)and(not(um)and(by[x,y+1]=1))  then move_P(x,y-1);
 move(tx,ty,tx1,ty1);
 end;
end;

{==================================================================}
{###################     MAIN   PROGRAM    ######################}
{==================================================================}
begin
 fs2:=paramstr(2);
 fs1:=paramstr(1);
 clrscr;
 if fs1='-solve' then goto 10;
10:
 assign(f1,fs2);
 reset(f1);
 for j:=1 to 4 do
  begin
   readln(f1,tm);
   for i:=1 to 4 do
   begin
    c:='';
    c:=copy(tm,(i-1)*3+1,3);
    if length(c)>2 then if c[3]=' ' then c[0]:=#2;
    if length(c)>1 then if c[2]=' ' then c[0]:=#1;
    if pos('x',c)<>0 then a[i,j]:=16
       else
        val(c,a[i,j],tmp);
    end;
  end;
 close(f1);
 strelki;
  chk_sum:=0;
   for i:=1 to 4 do
   for j:=1 to 4 do
   if a[i,j]<>16 then if not((bx[i,j]=0)and(by[i,j]=0)) then inc(chk_sum);
  if check then writeln('OK');
  {if (chk_sum mod 2=0)and(not(check)) then
   begin
    writeln('There is no solution of the problem');
    while keypressed do readkey;
    writeln('Press any key to exit ...');
    readkey;
    halt(1);
   end;}
 show;
 for i:=1 to 4 do
 for j:=1 to 4 do
  if a[i,j]=16 then
   begin
    k:=i;
    l:=j;
   end;
  move_p(k,l);
  show;
 readkey;
end.
