#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
const int MOD = 1e9 + 7;
const int bm = 1;
int n, m;
long long dem;
int len[30];
int f[30][N], g[30][N], a[30][N];
string s;
void sinh() {
  freopen(".inp", "w", stdout);
  srand(time(0));
}
int get(int first, int i) {
  int sum = 0;
  for (int j = i; j > 0; j -= (j & -j)) sum += g[first][j];
  return f[first][i] - sum;
}
void update(int first, int i, int w = 1) {
  for (int j = i; j <= n; j += (j & -j)) g[first][j] += w;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    int first = s[i] - 'a';
    len[first]++;
    a[first][len[first]] = i + 1;
    f[s[i] - 'a'][i + 1] = i + 1;
  }
  for (int i = 1; i <= n; i++) {
    int first = (int)(s[i - 1] - 'a');
    int m = get(first, a[first][len[first]]);
    dem += n - i + 1 - m;
    for (int j = 0; j < 26; j++) update(j, a[first][len[first]]);
    len[first]--;
  }
  cout << dem;
}
