#include <bits/stdc++.h>
using namespace std;
int n;
string s;
vector<int> pos[26];
vector<int> pos2[26];
int ct[26];
int fw[200005];
void up(int x, int nv) {
  while (x <= 200000) {
    fw[x] += nv;
    x += x & -x;
  }
}
int qu(int x) {
  int res = 0;
  while (x) {
    res += fw[x];
    x -= x & -x;
  }
  return res;
}
int main() {
  scanf("%d", &n);
  cin >> s;
  for (int i = 0; i < n; i++) {
    int c = s[i] - 'a';
    pos[c].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    up(i, 1);
  }
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    int c = s[i] - 'a';
    int j = pos[c][ct[c]];
    int k = qu(j + 1);
    up(1, 1);
    up(j + 1, -1);
    ct[c]++;
    int rev = n - i;
    ans += (k - rev);
  }
  printf("%lld", ans);
}
