#include <bits/stdc++.h>
using namespace std;
const int maxlongint = 2147483647;
const int inf = 1000000000;
int ch[200010][5], fa[200010], len[200010], now = 1, pre = 1;
void insert(int i) {
  now++;
  int np = now, p = pre;
  len[np] = len[p] + 1;
  pre = np;
  for (; p != 0 && ch[p][i] == 0; p = fa[p]) ch[p][i] = np;
  if (p == 0) {
    fa[np] = 1;
    return;
  }
  if (len[ch[p][i]] == len[p] + 1) {
    fa[np] = ch[p][i];
    return;
  }
  int q = ch[p][i];
  now++;
  int nq = now;
  fa[nq] = fa[q];
  fa[q] = fa[np] = nq;
  len[nq] = len[p] + 1;
  for (int n1 = 0; n1 < 4; n1++) ch[nq][n1] = ch[q][n1];
  for (; p != 0 && ch[p][i] == q; p = fa[p]) ch[p][i] = nq;
}
int v[200010], dp[200010][4];
void get(int i) {
  int n1, n2;
  if (v[i]) return;
  v[i] = 1;
  dp[i][0] = dp[i][1] = dp[i][2] = dp[i][3] = inf;
  for (n1 = 0; n1 <= 3; n1++)
    if (ch[i][n1] == 0)
      dp[i][n1] = 1;
    else {
      get(ch[i][n1]);
      for (n2 = 0; n2 <= 3; n2++)
        dp[i][n2] = min(dp[i][n2], dp[ch[i][n1]][n2] + 1);
    }
}
char s[100010];
long long n;
struct matrix {
  long long p[5][5];
  long long* operator[](int x) { return p[x]; }
};
matrix jzc(matrix a, matrix b) {
  matrix c;
  for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= 4; j++) c[i][j] = n + 1;
  for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= 4; j++)
      for (int k = 1; k <= 4; k++)
        c[i][j] = min(c[i][j], min(n, a[i][k] + b[k][j]));
  return c;
}
matrix go, no, f[70];
int getm(matrix a) {
  for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= 4; j++)
      if (a[i][j] < n) return 1;
  return 0;
}
int main() {
  long long ans = 0;
  cin >> n;
  int n1, n2, i;
  scanf("%s", s);
  int len = strlen(s);
  for (n1 = 0; n1 < len; n1++) insert(s[n1] - 65);
  get(1);
  for (n1 = 0; n1 <= 3; n1++)
    if (ch[1][n1])
      for (n2 = 0; n2 <= 3; n2++) go[n1 + 1][n2 + 1] = dp[ch[1][n1]][n2];
    else
      for (n2 = 0; n2 <= 3; n2++) go[n1 + 1][n2 + 1] = 0;
  f[0] = go;
  for (i = 1; (1ll << i) <= n; i++) f[i] = jzc(f[i - 1], f[i - 1]);
  for (n1 = 1; n1 <= 4; n1++)
    for (n2 = 1; n2 <= 4; n2++)
      if (n1 == n2)
        no[n1][n2] = 0;
      else
        no[n1][n2] = n + 1;
  int p = i - 1;
  for (i = p; i >= 0; i--)
    if (getm(jzc(no, f[i])) == 1) {
      ans += (1ll << i);
      no = jzc(no, f[i]);
    }
  cout << ans + 1 << endl;
  return 0;
}

### Prompt

Create a solution in Cpp for the following problem:

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;
const int maxlongint = 2147483647;
const int inf = 1000000000;
int ch[200010][5], fa[200010], len[200010], now = 1, pre = 1;
void insert(int i) {
  now++;
  int np = now, p = pre;
  len[np] = len[p] + 1;
  pre = np;
  for (; p != 0 && ch[p][i] == 0; p = fa[p]) ch[p][i] = np;
  if (p == 0) {
    fa[np] = 1;
    return;
  }
  if (len[ch[p][i]] == len[p] + 1) {
    fa[np] = ch[p][i];
    return;
  }
  int q = ch[p][i];
  now++;
  int nq = now;
  fa[nq] = fa[q];
  fa[q] = fa[np] = nq;
  len[nq] = len[p] + 1;
  for (int n1 = 0; n1 < 4; n1++) ch[nq][n1] = ch[q][n1];
  for (; p != 0 && ch[p][i] == q; p = fa[p]) ch[p][i] = nq;
}
int v[200010], dp[200010][4];
void get(int i) {
  int n1, n2;
  if (v[i]) return;
  v[i] = 1;
  dp[i][0] = dp[i][1] = dp[i][2] = dp[i][3] = inf;
  for (n1 = 0; n1 <= 3; n1++)
    if (ch[i][n1] == 0)
      dp[i][n1] = 1;
    else {
      get(ch[i][n1]);
      for (n2 = 0; n2 <= 3; n2++)
        dp[i][n2] = min(dp[i][n2], dp[ch[i][n1]][n2] + 1);
    }
}
char s[100010];
long long n;
struct matrix {
  long long p[5][5];
  long long* operator[](int x) { return p[x]; }
};
matrix jzc(matrix a, matrix b) {
  matrix c;
  for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= 4; j++) c[i][j] = n + 1;
  for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= 4; j++)
      for (int k = 1; k <= 4; k++)
        c[i][j] = min(c[i][j], min(n, a[i][k] + b[k][j]));
  return c;
}
matrix go, no, f[70];
int getm(matrix a) {
  for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= 4; j++)
      if (a[i][j] < n) return 1;
  return 0;
}
int main() {
  long long ans = 0;
  cin >> n;
  int n1, n2, i;
  scanf("%s", s);
  int len = strlen(s);
  for (n1 = 0; n1 < len; n1++) insert(s[n1] - 65);
  get(1);
  for (n1 = 0; n1 <= 3; n1++)
    if (ch[1][n1])
      for (n2 = 0; n2 <= 3; n2++) go[n1 + 1][n2 + 1] = dp[ch[1][n1]][n2];
    else
      for (n2 = 0; n2 <= 3; n2++) go[n1 + 1][n2 + 1] = 0;
  f[0] = go;
  for (i = 1; (1ll << i) <= n; i++) f[i] = jzc(f[i - 1], f[i - 1]);
  for (n1 = 1; n1 <= 4; n1++)
    for (n2 = 1; n2 <= 4; n2++)
      if (n1 == n2)
        no[n1][n2] = 0;
      else
        no[n1][n2] = n + 1;
  int p = i - 1;
  for (i = p; i >= 0; i--)
    if (getm(jzc(no, f[i])) == 1) {
      ans += (1ll << i);
      no = jzc(no, f[i]);
    }
  cout << ans + 1 << endl;
  return 0;
}

```