#include <bits/stdc++.h>
using namespace std;
struct segTree {
  vector<int> tree;
  int sz;
  segTree(int n) {
    sz = 1;
    while (sz < n) {
      sz <<= 1;
    }
    tree.assign(2 * sz - 1, 0);
  }
  void set(int x, int lx, int rx, int id) {
    if (rx - lx == 1) {
      tree[x] = 1;
      return;
    }
    int m = (lx + rx) >> 1;
    if (id < m) {
      set(2 * x + 1, lx, m, id);
    } else {
      set(2 * x + 2, m, rx, id);
    }
    tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
  }
  void set(int id) { set(0, 0, sz, id); }
  int get(int x, int lx, int rx, int l, int r) {
    if (lx >= r || rx <= l) {
      return 0;
    }
    if (lx >= l && rx <= r) {
      return tree[x];
    }
    int m = (lx + rx) >> 1;
    int s1 = get(2 * x + 1, lx, m, l, r);
    int s2 = get(2 * x + 2, m, rx, l, r);
    return s1 + s2;
  }
  int get(int l, int r) { return get(0, 0, sz, l, r); }
};
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  int n;
  cin >> n;
  string s;
  cin >> s;
  string rev = s;
  reverse(rev.begin(), rev.end());
  vector<vector<int>> pos(26);
  vector<int> its(26, 0);
  for (int i = 0; i < n; i++) {
    pos[rev[i] - 'a'].push_back(i);
  }
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    int id = pos[s[i] - 'a'][its[s[i] - 'a']];
    its[s[i] - 'a']++;
    v[i] = id;
  }
  segTree st(n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += st.get(v[i] + 1, n);
    st.set(v[i]);
  }
  cout << ans;
}
