#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define cd complex <double>
#define PI 3.14159265358979
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int jump_l[200005][25], jump_r[200005][25];
ll sum_l[200005][25], sum_r[200005][25];
int n;
string s;
int l[200005], r[200005], cnt_cir[200005], max_l[200005], min_r[200005];
int prefix[200005][27], suffix[200005][27];
int locc[200005][27], rocc[200005][27], last[30];
int cnt_cp[200005], cnt_cs[200005];
pii last_suff[200005][27], last_pref[200005][27];
bool comp(pii a, pii b){
return a > b;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> s; n = s.length();
ll rnd = 0; rnd++;
for (int i = 1; i <= n; i++){
for (int c = 1; c <= 26; c++){
locc[i][c] = last[c];
prefix[i][c] = prefix[i - 1][c];
}
last[s[i - 1] - 'a' + 1] = i;
for (int c = 1; c <= 26; c++) last_pref[i][c] = mp(last[c], c);
sort(last_pref[i] + 1, last_pref[i] + 27, comp);
prefix[i][s[i - 1] - 'a' + 1]++;
for (int c = 1; c <= 26; c++) cnt_cp[i] += (prefix[i][c] != 0);
l[i] = r[i] = i;
}
for (int c = 1; c <= 26; c++) last[c] = n + 1;
for (int i = n; i >= 1; i--){
for (int c = 1; c <= 26; c++){
rocc[i][c] = last[c];
suffix[i][c] = suffix[i + 1][c];
}
last[s[i - 1] - 'a' + 1] = i;
for (int c = 1; c <= 26; c++) last_suff[i][c] = mp(last[c], c);
sort(last_suff[i] + 1, last_suff[i] + 27);
suffix[i][s[i - 1] - 'a' + 1]++;
for (int c = 1; c <= 26; c++) cnt_cs[i] += (suffix[i][c] != 0);
}
ll ans = 0;
for (int i = 1; i <= n; i++){
jump_l[i][0] = n + 1;
cnt_cir[i] = 0; max_l[i] = 0; min_r[i] = n + 1;
for (int c = 1; c <= 26; c++){
if (prefix[r[i]][c] - prefix[l[i] - 1][c]) cnt_cir[i]++;
else{
max_l[i] = max(max_l[i], locc[i][c]);
min_r[i] = min(min_r[i], rocc[i][c]);
}
}
}
for (int cnt_c = 1; cnt_c <= cnt_cp[n]; cnt_c++){
for (int i = 1; i <= n; i++){
sum_r[i][0] = sum_l[i][0] = i;
if (cnt_cp[i] >= cnt_c) jump_r[i][0] = max(jump_r[i][0], rocc[i][last_pref[i][cnt_c].se]);
if (jump_r[i][0] == n + 1) jump_r[i][0] = n;
if (cnt_cs[i] >= cnt_c) jump_l[i][0] = min(jump_l[i][0], locc[i][last_suff[i][cnt_c].se]);
if (jump_l[i][0] == 0) jump_l[i][0] = 1;
}
for (int lay = 1; lay <= 17; lay++){
for (int i = 1; i <= n; i++){
if (jump_l[i][lay - 1] == 0) jump_l[i][lay] = 0;
else{
sum_l[i][lay] = sum_l[i][lay - 1] + sum_l[jump_l[i][lay - 1]][lay - 1];
jump_l[i][lay] = jump_l[jump_l[i][lay - 1]][lay - 1];
}
if (jump_r[i][lay - 1] == n + 1) jump_r[i][lay] = n + 1;
else{
sum_r[i][lay] = sum_r[i][lay - 1] + sum_r[jump_r[i][lay - 1]][lay - 1];
jump_r[i][lay] = jump_r[jump_r[i][lay - 1]][lay - 1];
}
}
}
for (int i = 1; i <= n; i++){
if ((l[i] == 1) && (r[i] == n)) continue;
if (cnt_cir[i] > cnt_c) continue;
int cur_l = l[i], cur_r = r[i];
for (int bit = 17; bit >= 0; bit--){
if ((jump_r[cur_r][bit] >= min_r[i]) || (jump_l[cur_l][bit] <= max_l[i])) continue;
ans -= sum_r[cur_r][bit]; ans += sum_l[cur_l][bit];
ans += ((ll) (n - 1) << bit);
cur_l = jump_l[cur_l][bit]; cur_r = jump_r[cur_r][bit];
}
ans += cur_l; ans -= cur_r; ans += (n - 1);
l[i] = jump_l[cur_l][0]; r[i] = jump_r[cur_r][0];
cnt_cir[i] = 0; max_l[i] = 0; min_r[i] = n + 1;
for (int c = 1; c <= 26; c++){
if (prefix[r[i]][c] - prefix[l[i] - 1][c]) cnt_cir[i]++;
else{
max_l[i] = max(max_l[i], locc[i][c]);
min_r[i] = min(min_r[i], rocc[i][c]);
}
}
}
}
cout << ans << "\n";
}