#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX_N = 2e5 + 20;
const long long MOD = 1e9 + 7;
int n;
char a[MAX_N];
char b[MAX_N];
int c[MAX_N];
int lowbit(int x) { return x & (-x); }
int sum(int x) {
  int res = 0;
  for (int i = x; i >= 1; i -= lowbit(i)) {
    res += c[i];
  }
  return res;
}
void add(int x, int v) {
  for (int i = x; i <= n + 1; i += lowbit(i)) {
    c[i] += v;
  }
}
int cnt[30][MAX_N];
int st[30];
int main() {
  scanf("%d", &n);
  scanf("%s", a + 1);
  for (int i = 1; i <= n; i++) {
    int tmp = a[i] - 'a';
    cnt[tmp][++cnt[tmp][0]] = i;
  }
  for (int i = 1, j = n; i <= n; i++, j--) {
    b[j] = a[i];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int v = b[i] - 'a';
    st[v]++;
    int p = cnt[v][st[v]];
    p = p + sum(p);
    ans = (ans + i - 1 + p - i);
    add(cnt[v][st[v]] + 1, -1);
    add(n + 1, 1);
  }
  printf("%lld\n", ans);
}
