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