1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 using namespace std;
  7 #define Maxn 100010
  8 #define LL long long
  9 #define INF 0xfffffff
 10 #define inf 1LL<<60
 11 
 12 LL n;
 13 int mymin(int x,int y) {return x<y?x:y;}
 14 
 15 struct node
 16 {
 17     int pre,son[6],step;
 18 }t[Maxn*2];
 19 bool vis[2*Maxn];
 20 
 21 int mn[2*Maxn][6];
 22 
 23 struct sam
 24 {
 25     int last,tot;
 26     void extend(int k)
 27     {
 28         int np=++tot,p=last;
 29         t[np].step=t[p].step+1;
 30         while(p&&!t[p].son[k])
 31         {
 32             t[p].son[k]=np;
 33             p=t[p].pre;
 34         }
 35         if(!p) t[np].pre=1;
 36         else
 37         {
 38             int q=t[p].son[k];
 39             if(t[q].step==t[p].step+1) t[np].pre=q;
 40             else
 41             {
 42                 int nq=++tot;
 43                 memcpy(t[nq].son,t[q].son,sizeof(t[nq].son));
 44                 t[nq].step=t[p].step+1;
 45                 t[nq].pre=t[q].pre;
 46                 t[q].pre=t[np].pre=nq;
 47                 while(p&&t[p].son[k]==q)
 48                 {
 49                     t[p].son[k]=nq;
 50                     p=t[p].pre;
 51                 }
 52             }
 53         }
 54         last=np;
 55     }
 56     void dfs(int x)
 57     {
 58         if(vis[x]) return;
 59         vis[x]=1;
 60         for(int i=1;i<=4;i++) mn[x][i]=INF;
 61         for(int i=1;i<=4;i++)
 62         {
 63             if(!t[x].son[i]) mn[x][i]=1;
 64             else
 65             {
 66                 dfs(t[x].son[i]);
 67                 for(int j=1;j<=4;j++) mn[x][j]=mymin(mn[x][j],mn[t[x].son[i]][j]+1);
 68             }
 69         }
 70     }
 71 }sam;
 72 
 73 char s[Maxn];
 74 
 75 struct Matrix
 76 {
 77     LL w[6][6];
 78     Matrix() {memset(w,0,sizeof(w));}
 79     inline friend Matrix operator * (const Matrix A,const Matrix B)
 80     {
 81         Matrix ret;
 82         for(int i=1;i<=4;i++)
 83          for(int j=1;j<=4;j++)
 84          {
 85             ret.w[i][j]=inf;
 86             for(int k=1;k<=4;k++) ret.w[i][j]=min(ret.w[i][j],A.w[i][k]+B.w[k][j]);
 87          }
 88         return ret;
 89     }
 90     inline friend Matrix operator ^ (const Matrix A,LL k)
 91     {
 92         Matrix ret,tmp=A;
 93         for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) ret.w[i][j]=(i==j)?1:0;
 94         for (;k;k>>=1,tmp=tmp*tmp) if(k&1) ret=ret*tmp;
 95         return ret;
 96     }
 97 }Q;
 98 
 99 bool check(LL x)
100 {
101     Matrix B=Q^x;
102     LL mn=inf;
103     for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) mn=min(mn,B.w[i][j]);
104     return mn>=n;
105 }
106 
107 int main()
108 {
109     scanf("%lld",&n);
110     scanf("%s",s);
111     int ll=strlen(s);
112     sam.tot=sam.last=1;
113     for(int i=0;i<ll;i++) sam.extend(s[i]-‘A‘+1);
114     memset(vis,0,sizeof(vis));
115     sam.dfs(1);
116     
117     for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) Q.w[i][j]=mn[t[1].son[i]][j];
118     
119     LL l=1,r=n,ans;
120     while(l<r)
121     {
122         LL mid=(l+r)>>1;
123         if(check(mid)) r=mid;
124         else l=mid+1;
125     }
126     printf("%lld\n",r);
127     return 0;
128 }