#include <bits/stdc++.h>
#define ll long long
#define rll register ll
#define cll const ll
#define N 500005
#define M 55
using namespace std;
inline ll read()
{
rll x=0;bool f=1;register char c=getchar();
while(c<48||c>57){if(c=='-') f=0;c=getchar();}
while(c>=48&&c<=57){x=x*10+(c^48);c=getchar();}
return f?x:-x;
}
inline void write(ll x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+48);
}
ll n,ttt,ans,idx,cntc,idxs=1,last=1,s[N],v[N],h[N],ne[N],sc[N],ch[N],len[N],cnt[M],t[N][M];
bitset<M> flag;
char str[N];
inline ll val(char c)
{
if(c>=65&&c<=90) return c-65;
return c-71;
}
inline void insert(ll x)
{
rll p=++idxs,q,nq;
len[p]=len[last]+1,s[p]=1;
while(last&&!t[last][x])
t[last][x]=p,last=sc[last];
if(!last)
{
sc[p]=1,last=p;
return;
}
q=t[last][x];
if(len[q]==len[last]+1)
{
sc[p]=q,last=p;
return;
}
nq=++idxs;
len[nq]=len[last]+1;
sc[nq]=sc[q],sc[q]=sc[p]=nq;
memcpy(t[nq],t[q],sizeof t[nq]);
while(t[last][x]==q)
t[last][x]=nq,last=sc[last];
last=p;
}
inline void add(ll x,ll y){v[++idx]=y,ne[idx]=h[x],h[x]=idx;}
inline bool check()
{
for(rll i=2;i<=cntc;i++)
if(!cnt[ch[i]]||cnt[ch[i]]!=cnt[ch[i-1]])
return 0;
return 1;
}
inline void dfs1(ll x)
{
for(rll i=h[x];~i;i=ne[i])
{
cll y=v[i];
dfs1(y),s[x]+=s[y];
}
}
inline void dfs2(ll x)
{
if(check()) ans+=s[x];
for(rll i=1;i<=cntc;i++)
{
cll y=t[x][ch[i]];
if(y) cnt[ch[i]]++,dfs2(y),cnt[ch[i]]--;
}
}
int main()
{
memset(h,-1,sizeof h);
n=read(),scanf("%s",str+1);
for(rll i=1,tmp;i<=n;i++)
{
tmp=val(str[i]);
if(!flag[tmp])
ch[++cntc]=tmp,flag[tmp]=1;
insert(tmp);
}
for(rll i=2;i<=idxs;i++) add(sc[i],i);
dfs1(1),dfs2(1);
write(ans);
return 0;
}