#include <stdio.h>
#include <ctype.h>

#define MAXL 100000
#define HSIZE 200003

static char text[MAXL];
static char *hash[HSIZE];
static unsigned full[HSIZE];
static unsigned inverse[100];

static char *
nextAlpha (char *pos)
{
  while (*pos
	 && !isalpha (*pos))
    pos++;

  return pos;
}

static void
addAlpha (char ch, unsigned *hval)
{
  *hval = (*hval * (unsigned) (toupper (ch))) % HSIZE;
}

static void
remAlpha (char ch, unsigned *hval)
{
  unsigned long long tmp = *hval;

  tmp *= inverse[(unsigned) (toupper (ch))];

  *hval = (unsigned) (tmp % HSIZE);
}

static void
nsdCoef (unsigned x, unsigned y, unsigned *xc, unsigned *yc)
{
  unsigned pyx, px, t;

  if (x == 0)
    {
      *xc = 0;
      *yc = 1;
      return;
    }

  if (x > y)
    abort ();

  nsdCoef (y % x, x, &pyx, &px);

  t = ((y / x) * pyx) % HSIZE;
  if (t > px)
    px += HSIZE;
  *xc = px - t;
  *yc = pyx;
}

static unsigned
countInverse (unsigned x)
{
  unsigned xc, pc;

  nsdCoef (x, HSIZE, &xc, &pc);

  if ((x * xc) % HSIZE != 1)
    abort ();

  return xc;
}

static unsigned n_letters[100];

static int
sameUptoPerm (char *p1, char *p2, unsigned k)
{
  unsigned i, a, b, n_nonzero = 0;
  char *ap1 = p1, *ap2 = p2;

  for (i = 0; i < k; i++)
    {
      a = (unsigned) toupper (*ap1);
      b = (unsigned) toupper (*ap2);

      n_letters[a]++;
      if (n_letters[a] == 1)
	n_nonzero++;
      else if (n_letters[a] == 0)
	n_nonzero--;

      n_letters[b]--;
      if (n_letters[b] == ~0u)
	n_nonzero++;
      else if (n_letters[b] == 0)
	n_nonzero--;

      ap1 = nextAlpha (ap1 + 1);
      ap2 = nextAlpha (ap2 + 1);
    }

  if (!n_nonzero)
    return 1;

  ap1 = p1;
  ap2 = p2;
  for (i = 0; i < k; i++)
    {
      a = (unsigned) toupper (*ap1);
      b = (unsigned) toupper (*ap2);
      n_letters[a]--;
      n_letters[b]++;
      ap1 = nextAlpha (ap1 + 1);
      ap2 = nextAlpha (ap2 + 1);
    }

  return 0;
}

static int
record (char *spos, unsigned hval, unsigned ver, unsigned k)
{
  while (full[hval] == ver)
    {
      if (sameUptoPerm (spos, hash[hval], k))
	return 1;

      hval++;
      if (hval == HSIZE)
	hval = 0;
    }

  full[hval] = ver;
  hash[hval] = spos;

  return 0;
}

int main (void)
{
  char *spos, *epos;
  unsigned i, k;
  unsigned hval, ver;

  for (i = 'A'; i <= 'Z'; i++)
    inverse[i] = countInverse (i);

  for (ver = 1; ; ver++)
    {
      gets (text);
      sscanf (text, "%d", &k);
      if (!k)
	return 0;
      gets (text);

      if (!*text)
	{
	  puts ("0");
	  continue;
	}
      epos = text - 1;
      spos = nextAlpha (text);

      hval = 1;
      for (i = 0; i < k; i++)
	{
	  epos = nextAlpha (epos + 1);
	  if (!*epos)
	    break;

	  addAlpha (*epos, &hval);
	}
      if (i < k)
	{
	  printf ("%d\n", strlen (text));
	  continue;
	}

      while (1)
	{
	  if (record (spos, hval, ver, k))
	    break;

	  epos = nextAlpha (epos + 1);
	  if (!*epos)
	    break;
	  addAlpha (*epos, &hval);
	  remAlpha (*spos, &hval);
	  spos = nextAlpha (spos + 1);
	}

      *epos = 0;
      printf ("%d\n", strlen (text));
    }
}
