#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, tot[26], p[maxn];
char s[maxn], t[maxn];
vector<int> pos[26];
int val[maxn];
inline void Add(int x) {
  for (; x <= n; x += x & -x) ++val[x];
}
inline int Ask(int x) {
  int res = 0;
  for (; x; x -= x & -x) res += val[x];
  return res;
}
int main() {
  scanf("%d %s", &n, s + 1);
  for (int i = 1; i <= n; i++) {
    pos[s[i] - 'a'].push_back(i);
    t[n - i + 1] = s[i];
  }
  for (int i = 1; i <= n; i++) p[i] = pos[t[i] - 'a'][tot[t[i] - 'a']++];
  long long ans = 0;
  for (int i = n; i; i--) {
    ans += Ask(p[i]);
    Add(p[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
