#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
inline long long read() {
  long long 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 * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n;
char s[N];
int a[N], v[N];
long long ans;
void msort(int l, int r) {
  if (l >= r) return;
  int mid = l + r >> 1;
  msort(l, mid);
  msort(mid + 1, r);
  int i = l, j = mid + 1, k = l;
  while (i <= mid && j <= r) {
    if (a[i] <= a[j])
      v[k++] = a[i++];
    else {
      v[k++] = a[j++];
      ans += (mid - i + 1);
    }
  }
  while (i <= mid) v[k++] = a[i++];
  while (j <= r) v[k++] = a[j++];
  for (int i = l; i <= r; i++) a[i] = v[i];
}
int main() {
  n = read();
  scanf("%s", s + 1);
  vector<vector<int> > v(26);
  for (int i = 1; i <= n; i++) {
    v[s[i] - 'a'].push_back(n - i + 1);
  }
  for (int i = 1; i <= n; i++) {
    a[i] = v[s[i] - 'a'].back();
    v[s[i] - 'a'].pop_back();
  }
  msort(1, n);
  printf("%lld\n", ans);
  return 0;
}
