#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 100005;
long long n;
char str[N];
int l;
bool usd[N << 4];
int ch[N << 4][4], buc[4][4][15], tot;
struct matrix {
  long long a[4][4];
  void init(long long v) {
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 4; j++) a[i][j] = v;
  }
} bse;
matrix operator*(matrix a, matrix b) {
  matrix c;
  c.init(n + 1);
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        c.a[i][j] = min(c.a[i][j], a.a[i][k] + b.a[k][j]);
  return c;
}
int fpow(int x, int y) {
  int ret = 1;
  if (y < 1) return 1;
  for (; y; y >>= 1, x = x * x)
    if (y & 1) ret = ret * x;
  return ret;
}
bool check(long long mid) {
  if (!mid) return false;
  mid--;
  matrix res = bse, cp = bse;
  for (; mid; mid >>= 1, cp = cp * cp)
    if (mid & 1) res = res * cp;
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      if (res.a[i][j] < n) return true;
  return false;
}
int main() {
  n = read();
  scanf("%s", str + 1);
  l = strlen(str + 1);
  int lim = min(l, 10) + 1;
  for (int i = 1; i <= l; i++) {
    int now = 0, fir = str[i] - 'A';
    for (int j = i; j <= min(i + lim - 1, l); j++) {
      int c = str[j] - 'A';
      if (!ch[now][c]) ch[now][c] = ++tot;
      now = ch[now][c];
      if (!usd[now]) usd[now] = true, buc[fir][c][j - i + 1]++;
    }
  }
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      for (int k = lim; k >= 2; k--)
        if (buc[i][j][k] != fpow(4, k - 2)) bse.a[i][j] = k - 1;
  long long l = 0, r = n + 1;
  while (l + 1 < r) {
    long long mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  printf("%lld\n", r);
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

Appleman and Toastman like games. Today they play a game with strings with the following rules. Firstly Toastman tells Appleman two strings s and t both consisting only of letters 'A', 'B', 'C', 'D'. Then Appleman must build string s as quickly as possible. Initially he has empty string, and in one second he can append to end of the current string any contiguous substring of t.

Now, Toastman and Appleman are beginning to play the game. Toastman has already told string t to Appleman, but he hasn't come up with string s yet. Toastman only thinks, that he should choose string s consisting of n characters. Of course, he wants to find the worst string for Appleman (such string, that Appleman will spend as much time as possible during the game). Tell Toastman, how much time will Appleman spend during the game if Toastman finds the worst string for him. You can assume that Appleman plays optimally, therefore he builds any string s in minimal possible time.

Input

The first line contains an integer n (1 ≤ n ≤ 1018). The second line contains string t (1 ≤ |t| ≤ 105). String t consists of only letters 'A', 'B', 'C', 'D'. Each letter appears at least once in string t.

Output

Print a single integer — the largest possible time Appleman needs.

Examples

Input

5
ABCCAD


Output

5


Input

5
AAABACADBABBBCBDCACBCCCDDDBDCDD


Output

4

Note

In the first example, Toastman can choose s equal to "AAAAA".

In the second example, Toastman can choose s equal to "DADDA".

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 100005;
long long n;
char str[N];
int l;
bool usd[N << 4];
int ch[N << 4][4], buc[4][4][15], tot;
struct matrix {
  long long a[4][4];
  void init(long long v) {
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 4; j++) a[i][j] = v;
  }
} bse;
matrix operator*(matrix a, matrix b) {
  matrix c;
  c.init(n + 1);
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        c.a[i][j] = min(c.a[i][j], a.a[i][k] + b.a[k][j]);
  return c;
}
int fpow(int x, int y) {
  int ret = 1;
  if (y < 1) return 1;
  for (; y; y >>= 1, x = x * x)
    if (y & 1) ret = ret * x;
  return ret;
}
bool check(long long mid) {
  if (!mid) return false;
  mid--;
  matrix res = bse, cp = bse;
  for (; mid; mid >>= 1, cp = cp * cp)
    if (mid & 1) res = res * cp;
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      if (res.a[i][j] < n) return true;
  return false;
}
int main() {
  n = read();
  scanf("%s", str + 1);
  l = strlen(str + 1);
  int lim = min(l, 10) + 1;
  for (int i = 1; i <= l; i++) {
    int now = 0, fir = str[i] - 'A';
    for (int j = i; j <= min(i + lim - 1, l); j++) {
      int c = str[j] - 'A';
      if (!ch[now][c]) ch[now][c] = ++tot;
      now = ch[now][c];
      if (!usd[now]) usd[now] = true, buc[fir][c][j - i + 1]++;
    }
  }
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      for (int k = lim; k >= 2; k--)
        if (buc[i][j][k] != fpow(4, k - 2)) bse.a[i][j] = k - 1;
  long long l = 0, r = n + 1;
  while (l + 1 < r) {
    long long mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  printf("%lld\n", r);
  return 0;
}

```