#include <bits/stdc++.h>
using namespace std;
const int N = 11 * 100100;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
int ncnt = 1;
int nxt[N][4];
vector<vector<long long>> f[64];
long long n;
string s;
int dep;
vector<vector<long long>> get_res(long long init = 0) {
  return vector<vector<long long>>(4, vector<long long>(4, init));
}
inline int ord(char c) { return c - 'A'; }
void insert(const string &t, int start, int finish) {
  int u = 1;
  for (int i = start; i < finish; ++i) {
    if (!nxt[u][ord(t[i])]) {
      nxt[u][ord(t[i])] = ++ncnt;
    }
    u = nxt[u][ord(t[i])];
  }
}
void dfs(int u, int d, int h, int begin, int cur) {
  if (u == 0) {
    f[0][begin][cur] = min(f[0][begin][cur], (long long)d - 1);
  }
  if (d == h) return;
  for (int i = 0; i < 4; ++i) {
    dfs(nxt[u][i], d + 1, h, begin, i);
  }
}
vector<vector<long long>> res_mul(const vector<vector<long long>> &a,
                                  const vector<vector<long long>> &b) {
  vector<vector<long long>> ans = get_res(INFLL);
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        ans[i][j] = min(ans[i][j], a[i][k] + b[k][j]);
      }
    }
  }
  return ans;
}
long long smallest(const vector<vector<long long>> &x) {
  long long ans = INFLL;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      ans = min(ans, x[i][j]);
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  cin >> s;
  dep = (33 - __builtin_clz(s.size() + 1)) / 2 + 2;
  for (int i = 0; i < s.size(); ++i) {
    insert(s, i, min(i + dep, (int)s.size()));
  }
  for (int i = 0; i < 64; ++i) {
    f[i] = get_res(INFLL);
  }
  for (int i = 0; i < 4; ++i) {
    dfs(nxt[1][i], 1, dep, i, i);
  }
  for (int l = 0; l < 63; ++l) {
    f[l + 1] = res_mul(f[l], f[l]);
  }
  long long amt = 0;
  vector<vector<long long>> cur = get_res();
  for (int i = 62; i >= 0; --i) {
    vector<vector<long long>> rm = res_mul(cur, f[i]);
    if (smallest(rm) < n) {
      amt += (1LL << i);
      cur = rm;
    }
  }
  assert(smallest(cur) < n);
  cur = res_mul(cur, f[0]);
  ++amt;
  assert(smallest(cur) >= n);
  cout << amt << "\n";
  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

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 N = 11 * 100100;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
int ncnt = 1;
int nxt[N][4];
vector<vector<long long>> f[64];
long long n;
string s;
int dep;
vector<vector<long long>> get_res(long long init = 0) {
  return vector<vector<long long>>(4, vector<long long>(4, init));
}
inline int ord(char c) { return c - 'A'; }
void insert(const string &t, int start, int finish) {
  int u = 1;
  for (int i = start; i < finish; ++i) {
    if (!nxt[u][ord(t[i])]) {
      nxt[u][ord(t[i])] = ++ncnt;
    }
    u = nxt[u][ord(t[i])];
  }
}
void dfs(int u, int d, int h, int begin, int cur) {
  if (u == 0) {
    f[0][begin][cur] = min(f[0][begin][cur], (long long)d - 1);
  }
  if (d == h) return;
  for (int i = 0; i < 4; ++i) {
    dfs(nxt[u][i], d + 1, h, begin, i);
  }
}
vector<vector<long long>> res_mul(const vector<vector<long long>> &a,
                                  const vector<vector<long long>> &b) {
  vector<vector<long long>> ans = get_res(INFLL);
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        ans[i][j] = min(ans[i][j], a[i][k] + b[k][j]);
      }
    }
  }
  return ans;
}
long long smallest(const vector<vector<long long>> &x) {
  long long ans = INFLL;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      ans = min(ans, x[i][j]);
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  cin >> s;
  dep = (33 - __builtin_clz(s.size() + 1)) / 2 + 2;
  for (int i = 0; i < s.size(); ++i) {
    insert(s, i, min(i + dep, (int)s.size()));
  }
  for (int i = 0; i < 64; ++i) {
    f[i] = get_res(INFLL);
  }
  for (int i = 0; i < 4; ++i) {
    dfs(nxt[1][i], 1, dep, i, i);
  }
  for (int l = 0; l < 63; ++l) {
    f[l + 1] = res_mul(f[l], f[l]);
  }
  long long amt = 0;
  vector<vector<long long>> cur = get_res();
  for (int i = 62; i >= 0; --i) {
    vector<vector<long long>> rm = res_mul(cur, f[i]);
    if (smallest(rm) < n) {
      amt += (1LL << i);
      cur = rm;
    }
  }
  assert(smallest(cur) < n);
  cur = res_mul(cur, f[0]);
  ++amt;
  assert(smallest(cur) >= n);
  cout << amt << "\n";
  return 0;
}

```