#include <bits/stdc++.h>
using namespace std;
long long n;
string s;
long long a[200010];
long long b[200010];
long long c[200010];
long long lowbit(long long x) { return x & (-x); }
void updata(long long i) {
  while (i <= n) {
    c[i]++;
    i += lowbit(i);
  }
}
long long getsum(long long i) {
  long long res = 0;
  while (i > 0) {
    res += c[i];
    i -= lowbit(i);
  }
  return res;
}
signed main() {
  while (~scanf("%lld", &n)) {
    memset(c, 0, sizeof(c));
    cin >> s;
    long long ans = 0;
    for (long long i = 0; i < 26; i++) {
      char ix = 'a' + i;
      long long cnt = 0;
      for (long long j = 0; j < n; j++) {
        if (s[j] == ix) {
          a[++cnt] = j + 1;
        }
      }
      for (long long j = 1; j <= cnt; j++) {
        b[a[j]] = n - a[cnt - j + 1] + 1;
      }
    }
    for (long long i = 1; i <= n; i++) {
      ans = ans + getsum(n) - getsum(b[i]);
      updata(b[i]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
