#include <bits/stdc++.h>
using namespace std;
const int MX = 200005;
const int inf = 2000000069;
int fwt[MX];
void upd(int ps, int v) {
  for (; ps < MX; ps += ps & -ps) fwt[ps] += v;
}
int que(int ps) {
  int rs = 0;
  for (; ps > 0; ps -= ps & -ps) rs += fwt[ps];
  return rs;
}
int n;
char s[MX];
queue<int> ps[26];
int main() {
  scanf("%d", &n);
  getchar();
  for (int i = 0; i < n; i++) {
    s[i] = getchar();
    ps[s[i] - 'a'].push(i + 1);
  }
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    int gt = ps[s[i] - 'a'].front();
    ps[s[i] - 'a'].pop();
    upd(gt, 1);
    ans += gt - que(gt);
  }
  printf("%lld\n", ans);
  return 0;
}
