#include <bits/stdc++.h>
#pragma GCC optimize(4)
using namespace std;
inline int gi() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return (f == 1) ? x : -x;
}
inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; }
const int maxn = 2e5 + 5;
int c[maxn], n;
char s[maxn];
long long ans;
set<int> now[26];
inline void update(int x, int v) {
  while (x <= n) {
    c[x] += v;
    x += x & -x;
  }
}
inline int query(int x) {
  int ret = 0;
  while (x) {
    ret += c[x];
    x -= x & -x;
  }
  return ret;
}
int main() {
  n = gi();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i) now[s[i] - 'a'].insert(i);
  reverse(s + 1, s + n + 1);
  for (int i = 1; i <= n; ++i) {
    int x = s[i] - 'a';
    int curr = *now[x].begin();
    now[x].erase(curr);
    ans += curr - i + query(curr);
    update(1, 1);
    update(curr + 1, -1);
  }
  printf("%lld\n", ans);
  return 0;
}
