#include <bits/stdc++.h>
using namespace std;
int n;
int c[200005];
char a[200005];
queue<int> q1[200005];
int lowbit(int x) { return x & (-x); }
void update(int x, int y) {
  while (x <= n + 1) {
    c[x] += y;
    x += lowbit(x);
  }
}
int getan(int x) {
  int res = 0;
  while (x > 0) {
    res += c[x];
    x -= lowbit(x);
  }
  return res;
}
int main() {
  memset(c, 0, sizeof(c));
  scanf("%d", &n);
  scanf("%s", a + 1);
  long long an = 0;
  int l = n;
  for (int i = 1; i <= l; i++) {
    q1[a[i] - 'a'].push(i);
  }
  for (int i = l; i > 0; i--) {
    int t1 = q1[a[i] - 'a'].front();
    q1[a[i] - 'a'].pop();
    an += t1 - getan(t1) - 1;
    update(t1, 1);
    update(n + 1, -1);
  }
  printf("%lld\n", an);
  return 0;
}
