#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long infinity = 1000000000000000000;
const int inf = 1e9 + 5;
bool do_debug = false;
struct segt {
  int tl;
  vector<int> t;
  int task(int child1, int child2) { return child1 + child2; }
  void update(int node, int l, int r, int pos, int val) {
    if (l == r) {
      t[node] += val;
    } else {
      int mid = (l + r) >> 1;
      if (pos <= mid) {
        update(node << 1, l, mid, pos, val);
      } else {
        update((node << 1) + 1, mid + 1, r, pos, val);
      }
      t[node] = task(t[node << 1], t[(node << 1) + 1]);
    }
  }
  void update(int pos, int val) { update(1, 0, tl - 1, pos, val); }
  int query(int node, int l, int r, int lq, int rq) {
    if (lq > rq) {
      return 0;
    } else if (lq <= l && rq >= r) {
      return t[node];
    } else {
      int mid = (l + r) >> 1;
      return task(query(node << 1, l, mid, lq, min(rq, mid)),
                  query((node << 1) + 1, mid + 1, r, max(lq, mid + 1), rq));
    }
  }
  int query(int lq, int rq) { return query(1, 0, tl - 1, lq, rq); }
  void build(vector<int>& v, int node, int l, int r) {
    if (l == r) {
      t[node] = v[l];
    } else {
      int mid = (l + r) >> 1;
      build(v, node << 1, l, mid);
      build(v, (node << 1) + 1, mid + 1, r);
      t[node] = task(t[node << 1], t[(node << 1) + 1]);
    }
  }
  void build(vector<int>& v) { build(v, 1, 0, tl - 1); }
};
long long cnt(vector<int>& p, int n) {
  segt t;
  t.tl = n + 1;
  t.t = vector<int>(t.tl << 2);
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans += t.query(1, p[i] - 1);
    t.update(p[i], 1);
  }
  return ans;
}
void solve() {
  int n;
  cin >> n;
  vector<char> v(n), r(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    r[i] = v[i];
  }
  reverse(r.begin(), r.end());
  queue<int> id[26];
  int x = 1;
  id[r[0] - 'a'].push(x);
  int prev = x;
  x++;
  for (int i = 1; i < n; i++) {
    if (r[i] == r[i - 1]) {
      id[r[i] - 'a'].push(prev);
    } else {
      id[r[i] - 'a'].push(x);
      prev = x;
      x++;
    }
  }
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    p[i] = id[v[i] - 'a'].front();
    id[v[i] - 'a'].pop();
  }
  long long ans = cnt(p, n);
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q = 1;
  while (q-- > 0) {
    solve();
  }
  return 0;
}
