#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 100;
queue<long long> s[150];
long long a[maxn], tree[maxn], n;
char str[maxn], t[maxn];
void add(long long x, long long d) {
  while (x <= n) {
    tree[x] += d;
    x += x & (-x);
  }
}
long long getsum(long long x) {
  long long sum = 0;
  while (x > 0) {
    sum += tree[x];
    x -= x & (-x);
  }
  return sum;
}
int main(void) {
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (long long i = 1; i <= n; i++) {
    s[str[i] - '0'].push(i);
  }
  for (long long i = 1; i <= n; i++) {
    t[i] = str[n - i + 1];
  }
  for (long long i = 1; i <= n; i++) {
    a[i] = s[t[i] - '0'].front();
    s[t[i] - '0'].pop();
  }
  long long res = 0;
  for (long long i = 1; i <= n; i++) {
    add(a[i], 1);
    res += (i - getsum(a[i]));
  }
  cout << res << endl;
  return 0;
}
