program g;
const HSize = 1000000;
type THashItem = record
                Next:longint;
		Occ:array['A'..'Z'] of byte;
		end;
var HashData:array[1..100000] of THashItem;
    Hash:array[0..HSize-1] of longint;
    Z:array[0..49] of char;
    Current:THashItem;
    HashSize:longint;
    K:longint;
    c:char;
    CN:longint;
    Complete:boolean;
    i:longint;
    ZBase:longint;
function HashFce(var Dat:THashItem):longint;
var Res:longint;
    c:char;
begin
   Res:=0;
   for c:='A' to 'Z' do 
      Res:=(Res*19+Dat.Occ[c]) mod HSize;
   HashFce:=Res;
end;
procedure Insert(var Dat:THashItem);
var l:longint;

begin
   inc(Hashsize);
   HashData[HashSize]:=Dat;
   l:=HashFce(Dat);
   HashData[HashSize].Next:=Hash[l];
   Hash[l]:=HashSize;
end;
function IsIn(var Dat:THashItem):boolean;
var l:longint;
begin
   l:=HAsh[HashFce(Dat)];
   while l<>-1 do begin
      if (Dat.Occ=HashData[l].Occ) then
	    break;
      l:=HashData[l].Next;
   end;
   IsIn:=(l<>-1);
end;
begin
   readln(K);
   while (K<>0) do begin
      for c:='A' to 'Z' do Current.Occ[c]:=0;
      for i:=0 to HSize-1 do 
         HAsh[i]:=-1;
      i:=0;CN:=0;HashSize:=0;
      while (i<k) do begin
         if eoln then break;
         read(c);inc(CN);
	 c:=UpCase(c);
	 if (c>='A') and (c<='Z') then begin
	    inc(Current.Occ[c]);
	    Z[i]:=c;
	    inc(i);
	 end;
      end;
      if eoln then begin
         writeln(CN);
	 continue;
      end;
      Insert(Current);
      ZBase:=0;
      Complete:=false;
      while not eoln do begin
         read(c);inc(CN);
	 c:=UpCase(c);
	 if (c>='A') and (c<='Z') then begin
	    dec(Current.Occ[Z[ZBase]]);
	    inc(Current.Occ[c]);
	    Z[ZBase]:=c;
	    ZBase:=(Zbase+1) mod k;
	    if IsIn(Current) then begin
	       writeln(CN-1);
	       readln;
	       Complete:=true;
	       break;
	    end;
	    Insert(Current);
	 end;
      end;
      if not Complete then
         writeln(CN-1);
      readln(K);
   end;
end.