#include <bits/stdc++.h>
using namespace std;
const int MAXLEN = 100000;
long long have;
char s[MAXLEN + 1];
int slen;
typedef struct Node {
  int start, end;
  int childs[4];
  int suffix;
  Node(int start, int end) : start(start), end(end) {
    memset(childs, -1, sizeof(childs));
    suffix = -1;
  }
} Node;
vector<Node> nodes;
int addnode(int start, int end) {
  nodes.push_back(Node(start, end));
  return ((int)(nodes).size()) - 1;
}
int create() {
  int root = addnode(0, 0);
  int atnode = root, atedge = 0, atlen = 0, rem = 0, prev = -1;
  for (int i = 0; i < slen; ++i) {
    ++rem;
    prev = -1;
    while (rem > 0) {
      int tonode = nodes[atnode].childs[s[atedge] - 'A'];
      int tostart = tonode == -1 ? -1 : nodes[tonode].start;
      if (tonode != -1 && nodes[tonode].start + atlen >= nodes[tonode].end) {
        atedge += nodes[tonode].end - nodes[tonode].start;
        atlen -= nodes[tonode].end - nodes[tonode].start;
        atnode = tonode;
        continue;
      }
      if (tonode == -1) {
        int leaf = addnode(i, slen);
        nodes[atnode].childs[s[atedge] - 'A'] = leaf;
        if (prev != -1) nodes[prev].suffix = atnode;
        prev = atnode;
      } else if (s[tostart + atlen] == s[i]) {
        ++atlen;
        if (prev != -1) nodes[prev].suffix = atnode;
        prev = atnode;
        break;
      } else {
        int branch = addnode(tostart, tostart + atlen);
        nodes[tonode].start += atlen;
        int leaf = addnode(i, slen);
        nodes[atnode].childs[s[atedge] - 'A'] = branch;
        nodes[branch].childs[s[tostart + atlen] - 'A'] = tonode;
        nodes[branch].childs[s[i] - 'A'] = leaf;
        if (prev != -1) nodes[prev].suffix = branch;
        prev = branch;
      }
      --rem;
      if (atnode == root) {
        if (atlen > 0) --atlen;
        atedge++;
      } else {
        atnode = nodes[atnode].suffix;
      }
    }
  }
  return root;
}
void print(int node, int depth) {
  for (int i = 0; i < depth; ++i) printf("  ");
  printf("'");
  for (int i = nodes[node].start; i < nodes[node].end; ++i) printf("%c", s[i]);
  printf("'");
  printf(" -> %d", node);
  if (nodes[node].suffix != -1) printf(" [%d]", nodes[node].suffix);
  puts("");
  for (int i = 0; i < 4; ++i)
    if (nodes[node].childs[i] != -1) print(nodes[node].childs[i], depth + 1);
}
int cst[4][4];
int bst[5][4][4];
void update(int a, int b, int cur) {
  if (cur < cst[a][b]) cst[a][b] = cur;
}
void dfs(int node, int a, int len) {
  int clen = nodes[node].end - nodes[node].start;
  if (clen > 1) {
    int x = s[nodes[node].start + 1] - 'A';
    for (int b = (0); b < (4); ++b) update(a, b, len + 2 + (b == x ? 1 : 0));
  } else
    for (int i = (0); i < (4); ++i) {
      if (nodes[node].childs[i] != -1)
        dfs(nodes[node].childs[i], a, len + 1);
      else
        for (int b = (0); b < (4); ++b)
          update(a, b, len + 2 + (b != i ? 1 : 0));
    }
}
void run() {
  scanf("%I64d%s", &have, s);
  slen = strlen(s);
  for (int i = (0); i < (4); ++i)
    for (int j = (0); j < (4); ++j) cst[i][j] = INT_MAX;
  int root = create();
  for (int i = (0); i < (4); ++i) dfs(nodes[root].childs[i], i, 0);
  for (int i = (0); i < (4); ++i)
    for (int j = (0); j < (4); ++j) bst[1][i][j] = cst[i][j];
  for (int len = (2); len <= (4); ++len)
    for (int i = (0); i < (4); ++i)
      for (int j = (0); j < (4); ++j) {
        bst[len][i][j] = INT_MAX;
        for (int k = (0); k < (4); ++k)
          if (bst[len - 1][i][k] + cst[k][j] - 1 < bst[len][i][j])
            bst[len][i][j] = bst[len - 1][i][k] + cst[k][j] - 1;
      }
  long long ret = 1;
  for (int i = (0); i < (4); ++i)
    for (int j = (0); j < (4); ++j)
      for (int k = (0); k < (4); ++k)
        for (int cyc = (1); cyc <= (4); ++cyc)
          for (int pre = (i == j ? 0 : 1); pre < (cyc); ++pre)
            for (int suf = (j == k ? 0 : 1); suf < (cyc); ++suf) {
              long long rem = have - 1;
              if (pre > 0) rem -= bst[pre][i][j] - 1;
              if (suf > 0) rem -= bst[suf][j][k] - 1;
              if (rem < 0) continue;
              long long times = rem / (bst[cyc][j][j] - 1);
              long long cur = pre + times * cyc + suf + 1;
              if (cur > ret) ret = cur;
            }
  printf("%I64d\n", ret);
}
int main() {
  run();
  return 0;
}

### Prompt

Your challenge is to write 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 int MAXLEN = 100000;
long long have;
char s[MAXLEN + 1];
int slen;
typedef struct Node {
  int start, end;
  int childs[4];
  int suffix;
  Node(int start, int end) : start(start), end(end) {
    memset(childs, -1, sizeof(childs));
    suffix = -1;
  }
} Node;
vector<Node> nodes;
int addnode(int start, int end) {
  nodes.push_back(Node(start, end));
  return ((int)(nodes).size()) - 1;
}
int create() {
  int root = addnode(0, 0);
  int atnode = root, atedge = 0, atlen = 0, rem = 0, prev = -1;
  for (int i = 0; i < slen; ++i) {
    ++rem;
    prev = -1;
    while (rem > 0) {
      int tonode = nodes[atnode].childs[s[atedge] - 'A'];
      int tostart = tonode == -1 ? -1 : nodes[tonode].start;
      if (tonode != -1 && nodes[tonode].start + atlen >= nodes[tonode].end) {
        atedge += nodes[tonode].end - nodes[tonode].start;
        atlen -= nodes[tonode].end - nodes[tonode].start;
        atnode = tonode;
        continue;
      }
      if (tonode == -1) {
        int leaf = addnode(i, slen);
        nodes[atnode].childs[s[atedge] - 'A'] = leaf;
        if (prev != -1) nodes[prev].suffix = atnode;
        prev = atnode;
      } else if (s[tostart + atlen] == s[i]) {
        ++atlen;
        if (prev != -1) nodes[prev].suffix = atnode;
        prev = atnode;
        break;
      } else {
        int branch = addnode(tostart, tostart + atlen);
        nodes[tonode].start += atlen;
        int leaf = addnode(i, slen);
        nodes[atnode].childs[s[atedge] - 'A'] = branch;
        nodes[branch].childs[s[tostart + atlen] - 'A'] = tonode;
        nodes[branch].childs[s[i] - 'A'] = leaf;
        if (prev != -1) nodes[prev].suffix = branch;
        prev = branch;
      }
      --rem;
      if (atnode == root) {
        if (atlen > 0) --atlen;
        atedge++;
      } else {
        atnode = nodes[atnode].suffix;
      }
    }
  }
  return root;
}
void print(int node, int depth) {
  for (int i = 0; i < depth; ++i) printf("  ");
  printf("'");
  for (int i = nodes[node].start; i < nodes[node].end; ++i) printf("%c", s[i]);
  printf("'");
  printf(" -> %d", node);
  if (nodes[node].suffix != -1) printf(" [%d]", nodes[node].suffix);
  puts("");
  for (int i = 0; i < 4; ++i)
    if (nodes[node].childs[i] != -1) print(nodes[node].childs[i], depth + 1);
}
int cst[4][4];
int bst[5][4][4];
void update(int a, int b, int cur) {
  if (cur < cst[a][b]) cst[a][b] = cur;
}
void dfs(int node, int a, int len) {
  int clen = nodes[node].end - nodes[node].start;
  if (clen > 1) {
    int x = s[nodes[node].start + 1] - 'A';
    for (int b = (0); b < (4); ++b) update(a, b, len + 2 + (b == x ? 1 : 0));
  } else
    for (int i = (0); i < (4); ++i) {
      if (nodes[node].childs[i] != -1)
        dfs(nodes[node].childs[i], a, len + 1);
      else
        for (int b = (0); b < (4); ++b)
          update(a, b, len + 2 + (b != i ? 1 : 0));
    }
}
void run() {
  scanf("%I64d%s", &have, s);
  slen = strlen(s);
  for (int i = (0); i < (4); ++i)
    for (int j = (0); j < (4); ++j) cst[i][j] = INT_MAX;
  int root = create();
  for (int i = (0); i < (4); ++i) dfs(nodes[root].childs[i], i, 0);
  for (int i = (0); i < (4); ++i)
    for (int j = (0); j < (4); ++j) bst[1][i][j] = cst[i][j];
  for (int len = (2); len <= (4); ++len)
    for (int i = (0); i < (4); ++i)
      for (int j = (0); j < (4); ++j) {
        bst[len][i][j] = INT_MAX;
        for (int k = (0); k < (4); ++k)
          if (bst[len - 1][i][k] + cst[k][j] - 1 < bst[len][i][j])
            bst[len][i][j] = bst[len - 1][i][k] + cst[k][j] - 1;
      }
  long long ret = 1;
  for (int i = (0); i < (4); ++i)
    for (int j = (0); j < (4); ++j)
      for (int k = (0); k < (4); ++k)
        for (int cyc = (1); cyc <= (4); ++cyc)
          for (int pre = (i == j ? 0 : 1); pre < (cyc); ++pre)
            for (int suf = (j == k ? 0 : 1); suf < (cyc); ++suf) {
              long long rem = have - 1;
              if (pre > 0) rem -= bst[pre][i][j] - 1;
              if (suf > 0) rem -= bst[suf][j][k] - 1;
              if (rem < 0) continue;
              long long times = rem / (bst[cyc][j][j] - 1);
              long long cur = pre + times * cyc + suf + 1;
              if (cur > ret) ret = cur;
            }
  printf("%I64d\n", ret);
}
int main() {
  run();
  return 0;
}

```