#include <bits/stdc++.h>
using namespace std;
string S, R;
int n, pos[26], cnt[26];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> S;
  S = " " + S;
  R = S;
  for (int i = 1; i <= n; i++) {
    int t = S[i] - 'a';
    if (pos[t] == 0) pos[t] = i;
    cnt[t]++;
  }
  long long ans = 0;
  for (int i = n; i; i--) {
    int t = R[i] - 'a';
    for (int j = 0; j < 26; j++) {
      if (j == t) continue;
      if (pos[j] > pos[t]) pos[j]--;
    }
    ans += pos[t] - 1;
    S.erase(pos[t], 1);
    cnt[t]--;
    if (cnt[t] == 0)
      pos[t] = 0;
    else {
      while (S[pos[t]] - 'a' != t) pos[t]++;
    }
  }
  cout << ans << '\n';
  return 0;
}
