program Project1;
var n,i,sc,pc,p,nc,z,j,mc:longint;
  a:array [1..1000005] of integer;
         naj:boolean;
begin
  nc:=0;
naj:=true;
  sc:=0;
  pc:=0;
  readln(n);
  read(a[1]);
  read(a[2]);
  sc:=a[2];
  pc:=a[1];
   if sc>pc then naj:=false;

  for i:=3 to n do begin
  read(a[i]);
  sc:=a[i];
  pc:=a[i-1];
  if sc>pc then naj:=false;
  end;

    i:=1;

  if naj= true then begin
    p:=a[n-1];
    a[n-1]:=a[n];
    a[n]:=p;
    for i:=1 to n do write(a[i],' ');
  end

  else

  begin
   while (i<n) and (naj=false) do begin
   z:=i;
   j:=n;
   while j>1 do begin
   if (a[i]<a[j]) and (a[j]>nc) then begin
     nc:=a[j];
     mc:=j;
     naj:=true;
   end;
    j:=j-1;
    end;
   i:=i+1;
   end;
    p:=a[z];
    a[z]:=nc;
    a[mc]:=p;
     for i:=1 to n do write(a[i],' ');
   end;
end.