program casino;
Uses sysutils;
{$H+}
const lung=1000000;
var  N,M,C,w,v,coppie,index:Int64;
     temp: AnsiString;
     stringa:array[0..lung] of char;
     S,S_ruotate:array[0..lung] of AnsiString;
     funz_errore : array[0..2000000] of Int64;
function LexicalMinRotation(var x: AnsiString):Int64;
var 
len,K,i,j:Int64;

begin
   len:=length(x); 
   for i:=0 to len do funz_errore[i]:=-1;
   K:=1; 
   for j:=2 to len do
           begin
             i:=funz_errore[j - k-1 ];
             while (i <> -1 ) and (x[j] <> x[(k + i )]) do
                            begin
                              if x[j] < x[(k + i )] then k:= j - i-1 ;
                              i:= funz_errore[i+1];
                            end;                           
            if (i = -1) and (x[j] <> x[(k + i )]) then
                                                       begin
                                                          if x[j] < x[(k + i )] then k:= j;
                                                          funz_errore[j - k]:= -1;
                                                       end           
                                                    else   funz_errore[j - k]:= i + 1;
               
         end;   
 LexicalMinRotation:=k;
   
end;

begin
   (*assign(input, 'input.txt'); reset(input);
   assign(output, 'output.txt'); rewrite(output);*)
   readln (N,M);
   for w:=0 to N-1 do begin readln(S[w]);  S[w]:=Trim(S[w]); end;
   coppie:=0; 
   for w:=0 to N-1 do
          begin
            index:=LexicalMinRotation(S[w]);
            temp:=S[w]+S[w];
            S_ruotate[w]:=copy(temp,index,M);
            writeln (S_ruotate[w]); 
          end;
    for w:=0 to N-2 do  
             for v:=w+1 to N-1 do
                        begin
                          C:= CompareStr(S_ruotate[w], S_ruotate[v]);
                          if C=0 then coppie:=coppie+1;
                        end; 
    writeln (coppie);
end.