#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int T;
int N, K;
char S[1000100];
int a[1000100], b[1000100];
LL cnt = 0;
void merge(int l, int r) {
  if (l >= r) return;
  int mid = (l + r) >> 1;
  merge(l, mid), merge(mid + 1, r);
  int i = l, j = mid + 1, k = 0;
  while (i <= mid && j <= r)
    if (a[i] <= a[j])
      b[k++] = a[i++];
    else
      b[k++] = a[j++], cnt += mid - i + 1;
  while (i <= mid) b[k++] = a[i++];
  while (j <= r) b[k++] = a[j++];
  for (int i = 0, j = l; j <= r; j++, i++) a[j] = b[i];
  return;
}
void add(vector<int> &bit, int pos, int val) {
  for (int i = pos; i < bit.size(); i += i & (-i)) {
    bit[i] += val;
  }
}
int query(vector<int> &bit, int pos) {
  int ans = 0;
  for (int i = pos; i; i -= i & (-i)) {
    ans += bit[i];
  }
  return ans;
}
int main() {
  scanf("%d %s", &N, S);
  string s(S), ss;
  ss = s;
  reverse(ss.begin(), ss.end());
  vector<int> pos[256];
  int idxes[256] = {0};
  for (int i = 0; i < s.size(); ++i) {
    pos[s[i]].push_back(i + 1);
  }
  int an = 0;
  vector<int> v;
  map<int, int> val2pos;
  for (int i = 0; i < ss.size(); ++i) {
    int c = ss[i];
    a[an++] = pos[c][idxes[c]++];
    v.push_back(a[an - 1]);
    val2pos[a[an - 1]] = v.size();
  }
  merge(0, an - 1);
  vector<int> bit(ss.size() + 1, 0);
  LL ans = 0;
  for (auto p : val2pos) {
    ans += query(bit, ss.size()) - query(bit, p.second);
    add(bit, p.second, 1);
  }
  printf("%lld\n", ans);
  return 0;
}
