#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)2e5 + 10, L = 4, INF = (int)1e9;   struct State {     int fa, len, tr[L], w[L];   } SAM[MAXN];   char t[MAXN];   vector<int> l[MAXN];   int m, tot;   unsigned long long a[L], b[L][L], c[L][L];   unsigned long long n;   int Build(int pre, int id) {     int cp = ++tot;     SAM[cp].len = SAM[pre].len + 1;     for (; !SAM[pre].tr[id] && pre; SAM[pre].tr[id] = cp, pre = SAM[pre].fa)       ;     if (pre)       if (SAM[pre].len + 1 == SAM[SAM[pre].tr[id]].len)         SAM[cp].fa = SAM[pre].tr[id];       else {         int tp = SAM[pre].tr[id];         ++tot, SAM[tot].len = SAM[pre].len + 1, SAM[tot].fa = SAM[tp].fa;         memcpy(SAM[tot].tr, SAM[tp].tr, sizeof(SAM[tot].tr));         SAM[cp].fa = SAM[tp].fa = tot;         for (; pre && SAM[pre].tr[id] == tp;              SAM[pre].tr[id] = tot, pre = SAM[pre].fa)           ;       }     else       SAM[cp].fa = 1;     return cp;   }   void init() {     cin >> n;     scanf( %s , t + 1);     m = strlen(t + 1);     tot = 1;     for (int i = 1, p = 1; i <= m; ++i) p = Build(p, t[i] -  A );     for (int i = 1; i <= tot; ++i) {       for (int j = 0; j < L; ++j)         if (!SAM[i].tr[j])           SAM[i].w[j] = 1;         else           SAM[i].w[j] = INF;       l[SAM[i].len].push_back(i);     }     for (int i = m; i >= 0; --i)       for (int j = 0; j < (int)l[i].size(); ++j)         for (int k = 0; k < L; ++k)           if (SAM[l[i][j]].tr[k])             for (int p = 0; p < L; ++p) {               int first = l[i][j];               SAM[first].w[p] =                   min(SAM[first].w[p], SAM[SAM[first].tr[k]].w[p] + 1);             }   }   void mul(unsigned long long a[][L], unsigned long long b[][L],            unsigned long long c[][L]) {     static unsigned long long w[L][L];     for (int i = 0; i < L; ++i)       for (int j = 0; j < L; ++j) w[i][j] = n + 1;     for (int i = 0; i < L; ++i)       for (int j = 0; j < L; ++j)         for (int k = 0; k < L; ++k) w[i][j] = min(w[i][j], b[i][k] + c[k][j]);     for (int i = 0; i < L; ++i)       for (int j = 0; j < L; ++j) a[i][j] = w[i][j];   }   void Pow(unsigned long long a[][L], unsigned long long b) {     static unsigned long long d[L][L];     for (int i = 0; i < L; ++i)       for (int j = 0; j < L; ++j) d[i][j] = n + 1;     for (int i = 0; i < L; ++i) d[i][i] = 0;     for (; b; mul(a, a, a), b /= 2)       if (b & 1) mul(d, d, a);     memcpy(a, d, sizeof(d));   }   unsigned long long check(unsigned long long k) {     memcpy(b, c, sizeof(b));     Pow(b, k - 2);     unsigned long long Tohka = n + 1;     for (int i = 0; i < L; ++i)       for (int j = 0; j < L; ++j) Tohka = min(Tohka, a[i] + b[i][j]);     return Tohka;   }   unsigned long long Find(unsigned long long l, unsigned long long r) {     while (l <= r) {       unsigned long long mid = (l + r) >> 1;       if (check(mid) > n)         r = mid - 1;       else         l = mid + 1;     }     return r;   }   void solve() {     for (int i = 0; i < L; ++i) a[i] = max(SAM[1].w[i], 1);     for (int i = 0; i < L; ++i)       for (int j = 0; j < L; ++j) c[i][j] = max(SAM[SAM[1].tr[i]].w[j], 1);     if (n == 1)       puts( 1 );     else       cout << Find(2, n) << endl;   }   int main() {     init();     solve();     fclose(stdin);     fclose(stdout);     return 0;   }