#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long int INF = 9e18 + 2e17;
const int inf = 2e9;
const double eps = 1e-10;
struct SegTree {
  vector<int> tree;
  SegTree(vector<int>& arr) {
    int n = (int)(arr).size();
    tree = vector<int>(5 * n + 2);
    _build(0, 0, n - 1, arr);
  }
  void _build(int idx, int l, int r, vector<int>& arr) {
    if (l == r) {
      tree[idx] = arr[l];
      return;
    }
    int m = (l + r) / 2;
    _build(2 * idx + 1, l, m, arr);
    _build(2 * idx + 2, m + 1, r, arr);
    tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2];
  }
  void _update(int idx, int l, int r, int s, int e, int v) {
    if (s > r || e < l || s > r) return;
    if (s >= l and e <= r) {
      tree[idx] = v;
      return;
    }
    int m = (s + e) / 2;
    _update(2 * idx + 1, l, r, s, m, v);
    _update(2 * idx + 2, l, r, m + 1, e, v);
    tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2];
  }
  int _query(int idx, int l, int r, int s, int e) {
    if (s > r || e < l || s > e) return 0;
    if (s >= l and e <= r) return tree[idx];
    int m = (s + e) / 2;
    int y = _query(2 * idx + 1, l, r, s, m);
    int x = _query(2 * idx + 2, l, r, m + 1, e);
    return x + y;
  }
  void update(int l, int r, int n, int v) { _update(0, l, r, 0, n - 1, v); }
  int query(int l, int r, int n) { return _query(0, l, r, 0, n - 1); }
  void show() {
    for (int i = (int)0; i < (int)10; i++) cout << tree[i] << " ";
    cout << endl;
  }
};
int main(int argc, char const* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  queue<int> q[26];
  string s;
  cin >> s;
  for (int i = (int)0; i < (int)n; i++) {
    q[s[i] - 'a'].push(i);
  }
  reverse((s).begin(), (s).end());
  vector<int> from(n);
  long long int ans = 0;
  SegTree tree(from);
  for (int i = (int)0; i < (int)n; i++) {
    char c = s[i];
    int pos = q[c - 'a'].front();
    q[c - 'a'].pop();
    int val = tree.query(pos, pos, n);
    tree.update(pos, pos, n, val + 1);
    int shift = tree.query(pos + 1, n - 1, n);
    pos += shift;
    ans += pos - i;
  }
  cout << ans << endl;
  return 0;
}
