#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n, p[maxn], tr[maxn];
char s[maxn];
vector<int> id[maxn];
inline void add(int x, int y) {
  for (; x <= n; x += ((x) & (-(x)))) tr[x] += y;
}
inline int sum(int x) {
  int ans = 0;
  for (; x; x -= ((x) & (-(x)))) ans += tr[x];
  return ans;
}
int main() {
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++) id[s[i] - 'a'].push_back(i);
  reverse(s + 1, s + n + 1);
  long long ans = 0;
  for (int i = n; i; i--) {
    p[i] = id[s[i] - 'a'].back();
    id[s[i] - 'a'].pop_back();
    ans += sum(p[i] - 1), add(p[i], 1);
  }
  printf("%lld\n", ans);
  return 0;
}
