#include <bits/stdc++.h>
using namespace std;
long long query(vector<int>& items, int lo, int hi) {
  if (lo >= hi) {
    return 0;
  }
  int mid = (lo + hi) / 2;
  auto count = query(items, lo, mid) + query(items, mid + 1, hi);
  vector<int> temp(hi - lo + 1, 0);
  for (int k = 0, i = lo, j = mid + 1; k < int(temp.size()); k++) {
    if (j > hi || (i <= mid && items[i] <= items[j])) {
      temp[k] = items[i++];
    } else {
      temp[k] = items[j++];
      count += (mid - i + 1);
    }
  }
  for (int k = 0; k < int(temp.size()); k++) {
    items[lo + k] = temp[k];
  }
  return count;
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  string rev(s.rbegin(), s.rend());
  vector<int> revPos(n);
  map<char, set<int>> pos;
  for (int i = 0; i < n; i++) {
    pos[s[i]].insert(i);
  }
  for (int i = 0; i < n; i++) {
    auto& avail = pos[rev[i]];
    auto iter = avail.begin();
    revPos[*iter] = i;
    avail.erase(iter);
  }
  cout << query(revPos, 0, int(revPos.size() - 1)) << endl;
}
