#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k;
cin>>k;
string s;
cin>>s;
int n = s.length();
int N[n];
N[0] = 0;
for(int i = 1;i < n;++i) {
int p = N[i - 1];
while(p != 0 && s[p] != s[i]){
p = N[p - 1];
}
if(s[p] == s[i]) {
p++;
}
N[i] = p;
}
long long ans = 0;
for(int i = n - 1;i >= 0;--i) {
int j = N[i];
if(j == 0) continue;
while(N[j - 1]) j = N[j - 1];
N[i] = j;
ans += i - j + 1;
}
cout<<ans;
return 0;
}