#include <bits/stdc++.h>
using namespace std;
int head[30], now[30], cnt;
char t[200010];
int a[200010], b[200010], n;
long long ans;
struct edge {
  int next, to;
} e[200010];
void add(int x, int y) {
  e[++cnt] = (edge){head[x], y};
  head[x] = cnt;
}
void mergesort(int l, int r) {
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  mergesort(l, mid);
  mergesort(mid + 1, r);
  for (int i = l; i <= r; i++) {
    b[i] = a[i];
  }
  int lx = l, rx = mid + 1, k = l;
  while (lx <= mid && rx <= r) {
    if (b[lx] > b[rx]) {
      a[k++] = b[rx++];
      ans += mid - lx + 1;
    } else {
      a[k++] = b[lx++];
    }
  }
  while (lx <= mid) {
    a[k++] = b[lx++];
  }
  while (rx <= r) {
    a[k++] = b[rx++];
  }
}
void debug() {
  for (int i = 1; i <= n; i++) {
    printf("%d ", a[i]);
  }
  printf("\n");
}
int main() {
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%c", &t[i]);
    int x = t[i] - 'a';
    add(x, n + 1 - i);
    now[x] = head[x];
  }
  for (int i = 1; i <= n; i++) {
    int x = t[i] - 'a';
    a[i] = e[now[x]].to;
    now[x] = e[now[x]].next;
  }
  mergesort(1, n);
  printf("%lld", ans);
  return 0;
}
