#include <bits/stdc++.h>
using namespace std;
const long long inf = 1000000000000000000ll;
long long n;
int len;
long long f[70][4][4], g[4][4];
int nxt[100005 * 13][4], sum[4][4][14], ndsum;
bool vis[100005 * 13];
char a[100005];
void ins(int pos) {
  int now = 0, fir = a[pos] - 'A';
  for (int i = 1; i <= 13 && pos <= len; ++i, ++pos) {
    int c = a[pos] - 'A';
    if (!nxt[now][c]) nxt[now][c] = ++ndsum;
    now = nxt[now][c];
    if (!vis[now]) {
      vis[now] = 1;
      sum[fir][c][i]++;
    }
  }
}
long long solve(long long n) {
  int now = 1;
  memset(g, 0, sizeof(g));
  while (n) {
    if (n & 1) {
      long long pre[4][4];
      for (int i = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j) pre[i][j] = g[i][j];
      for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
          long long tmp = inf;
          for (int k = 0; k < 4; ++k) tmp = min(tmp, f[now][i][k] + pre[k][j]);
          g[i][j] = tmp;
        }
      }
    }
    n >>= 1;
    ++now;
  }
  long long ret = inf;
  for (int i = 0; i < 4; ++i)
    for (int j = 0; j < 4; ++j) ret = min(ret, g[i][j]);
  return ret;
}
int main() {
  scanf("%lld%s", &n, a + 1);
  len = strlen(a + 1);
  for (int i = 1; i <= len; ++i) ins(i);
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int l = 2, t = 1; l <= 13; ++l, t *= 4) {
        if (sum[i][j][l] < t) {
          f[1][i][j] = l - 1;
          break;
        }
      }
    }
  }
  for (int t = 2; t <= 64; ++t) {
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 4; ++j) {
        f[t][i][j] = inf;
        for (int k = 0; k < 4; ++k)
          f[t][i][j] = min(f[t][i][j], f[t - 1][i][k] + f[t - 1][k][j]);
      }
    }
  }
  long long l = 1, r = n, ans = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    long long tmp = solve(mid);
    if (tmp < n)
      l = mid + 1;
    else
      r = mid - 1, ans = mid;
  }
  printf("%lld\n", ans);
  return 0;
}

### Prompt

Your task is to create a Cpp solution to 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 long long inf = 1000000000000000000ll;
long long n;
int len;
long long f[70][4][4], g[4][4];
int nxt[100005 * 13][4], sum[4][4][14], ndsum;
bool vis[100005 * 13];
char a[100005];
void ins(int pos) {
  int now = 0, fir = a[pos] - 'A';
  for (int i = 1; i <= 13 && pos <= len; ++i, ++pos) {
    int c = a[pos] - 'A';
    if (!nxt[now][c]) nxt[now][c] = ++ndsum;
    now = nxt[now][c];
    if (!vis[now]) {
      vis[now] = 1;
      sum[fir][c][i]++;
    }
  }
}
long long solve(long long n) {
  int now = 1;
  memset(g, 0, sizeof(g));
  while (n) {
    if (n & 1) {
      long long pre[4][4];
      for (int i = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j) pre[i][j] = g[i][j];
      for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
          long long tmp = inf;
          for (int k = 0; k < 4; ++k) tmp = min(tmp, f[now][i][k] + pre[k][j]);
          g[i][j] = tmp;
        }
      }
    }
    n >>= 1;
    ++now;
  }
  long long ret = inf;
  for (int i = 0; i < 4; ++i)
    for (int j = 0; j < 4; ++j) ret = min(ret, g[i][j]);
  return ret;
}
int main() {
  scanf("%lld%s", &n, a + 1);
  len = strlen(a + 1);
  for (int i = 1; i <= len; ++i) ins(i);
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int l = 2, t = 1; l <= 13; ++l, t *= 4) {
        if (sum[i][j][l] < t) {
          f[1][i][j] = l - 1;
          break;
        }
      }
    }
  }
  for (int t = 2; t <= 64; ++t) {
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 4; ++j) {
        f[t][i][j] = inf;
        for (int k = 0; k < 4; ++k)
          f[t][i][j] = min(f[t][i][j], f[t - 1][i][k] + f[t - 1][k][j]);
      }
    }
  }
  long long l = 1, r = n, ans = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    long long tmp = solve(mid);
    if (tmp < n)
      l = mid + 1;
    else
      r = mid - 1, ans = mid;
  }
  printf("%lld\n", ans);
  return 0;
}

```