program wardrobe3;
Uses Strutils;
const MAX = 5000000;
type elenco = array[1..MAX] of string[1];
var m,k,i,PP, z :qword;
    numero, count : qword;
    cod : longint;
    cifre, X, stringadainvertire, stringainvertita, stringaordinata, Sconinv : Ansistring;
    ccifre : array[1..MAX] of integer;
    potenzadieci : array [0..MAX] of qword;
    arrnum, P :array [1..MAX] of  string[1];
    w:char;
    
procedure scambia (var x,y: string);
var t:string;
begin
   t:=x;
   x:=y;
   y:=t;
end;  
procedure scambiach (var x,y: char);
var t:char;
begin
   t:=x;
   x:=y;
   y:=t;
end; 
Procedure ordinamento (estremoi,estremos: qword; var v : elenco; ordinato:boolean);
var inf, sup, medio:qword;
    pivot :string[1];
begin
    inf:=estremoi;
    sup:=estremos;
    medio:= (estremoi+estremos) div 2;
    pivot:=v[medio];
    repeat
      if (ordinato) then
         begin
            while (v[inf]<pivot) do  inf:=inf+1;
            while (v[sup]>pivot) do  sup:=sup-1;
         end;
      if inf<=sup then
       begin
         scambia(v[inf],v[sup]);
         inf:=inf+1;
         sup:=sup-1;
       end;
    until inf>sup;
    if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
    if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
end; 
 Procedure inversione (x:Ansistring; inizio:qword;  var Sconinv:ansistring);
 var Sdainv,Sprefix : ansistring;
 begin
     count:=m-inizio+1; Sprefix:=''; 
     Sdainv:=copy(x,inizio, count); 
     Sprefix:=copy(x, 1, inizio-1); 
     stringainvertita:=ReverseString(Sdainv);
     Sconinv:=Sprefix+stringainvertita;
 end;  

Procedure nextPermutation (var P : ansistring) ;
var pivot:qword;   
   (* Find the pivot index*)
begin 
    pivot := 0; 
    for i := m - 1 downto 1 do
                    if (P[i] < P[i + 1]) then begin pivot:=i; break; end;
                    writeln(pivot);
    if pivot = 0 then exit;
       (* find the element from the right that is greater than pivot*)
    for i := m  downto pivot+1 do
                      begin
                          if (P[i] > P[pivot])  then scambiach (P[i], P[pivot]); 
                          writeln(P);
                          break;
                      end;
                  
    (* Reverse the elements from pivot + 1 to the end to get the next permutation*)
    if pivot+1=m then  begin Sconinv:=P; writeln('VV',Sconinv); end
                 else inversione (P, pivot+1, Sconinv);
    
    
     
    
end;
       
begin
	readln(m,k);
	readln(cifre);
             (*  X:=ReverseString(cifre); writeln (X);*)
	for i:=1 to m do arrnum[i]:=copy(cifre,i,1);
    ordinamento (1,m,arrnum, true);
    stringaordinata:=''; Sconinv:='';
    for i:=1 to m do stringaordinata:=stringaordinata+arrnum[i];
    (*val(cifre,numero,cod); 
    writeln (numero);
    inversione (cifre, 3, stringainvertita);
    writeln(stringainvertita);*)
    stringadainvertire:=stringaordinata;
    for i:=1 to m do write(arrnum[i]); writeln('oo');
    
    for z:=1 to 6 do begin  nextPermutation(stringadainvertire); stringadainvertire:=Sconinv; writeln('cicl',Sconinv); end;
end.
