#include <bits/stdc++.h>
using namespace std;
void update(vector<int> &tree, int node, int b, int e, int idx) {
  if (idx < b || idx > e) return;
  if (b == e && idx == b) {
    tree[node] = 1;
    return;
  }
  int m = b + (e - b) / 2;
  update(tree, 2 * node + 1, b, m, idx);
  update(tree, 2 * node + 2, m + 1, e, idx);
  tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
int query(vector<int> &tree, int node, int b, int e, int l, int r) {
  if (r < b || e < l) return 0;
  if (l <= b && e <= r) return tree[node];
  int m = b + (e - b) / 2;
  return query(tree, 2 * node + 1, b, m, l, r) +
         query(tree, 2 * node + 2, m + 1, e, l, r);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<int> > v(26);
  vector<int> idx(26);
  for (int i = 0; i < n; i++) {
    int idx2 = int(s[i]) - int('a');
    v[idx2].push_back(i);
  }
  vector<int> tree(4 * n);
  long long int res = 0;
  for (int i = n - 1; i >= 0; i--) {
    int idx2 = int(s[i]) - int('a');
    int j = n - 1 - i, k = query(tree, 0, 0, n - 1, v[idx2][idx[idx2]], n - 1);
    res = res + abs(j - v[idx2][idx[idx2]] - k);
    update(tree, 0, 0, n - 1, v[idx2][idx[idx2]]);
    idx[idx2]++;
  }
  cout << res << endl;
  return 0;
}
