#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops", \
                     "omit-frame-pointer", "inline")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC option("arch=native", "no-zero-upper")
using namespace std;
const long long oo = (long long)(1e9);
vector<int> pos[26];
mt19937 rng(time(0));
template <typename T>
struct treap {
  struct node {
    T key;
    int sz;
    int prior;
    node *l, *r;
    node(T key) : l(0), r(0), key(key), sz(1), prior(rng()) {}
  };
  typedef node *pnode;
  pnode root;
  treap() : root(0) {}
  int sz(pnode p) { return p ? p->sz : 0; }
  void update(pnode p) {
    if (p) p->sz = 1 + sz(p->l) + sz(p->r);
  }
  void split(pnode p, T key, pnode &l, pnode &r) {
    if (!p)
      l = r = 0;
    else if (key < p->key) {
      split(p->l, key, l, p->l);
      r = p;
    } else {
      split(p->r, key, p->r, r);
      l = p;
    }
    update(p);
  }
  void insert(T key) {
    pnode add = new node(key);
    insert(root, add);
  }
  void erase(T key) {
    int pos = order_of_key(key);
    if (pos && key == find_kth(pos)) erase(root, key);
  }
  int order_of_key(T key) { return order_of_key(root, key); }
  T find_kth(int k) { return find_kth(root, k); }
  int size() { return sz(root); }
  int order_of_key(pnode p, T key) {
    if (!p) return 0;
    if (p->key <= key) return sz(p->l) + 1 + order_of_key(p->r, key);
    return order_of_key(p->l, key);
  }
  T find_kth(pnode p, int k) {
    int pos = sz(p->l) + 1;
    if (pos == k) return p->key;
    if (pos > k) return find_kth(p->l, k);
    return find_kth(p->r, k - pos);
  }
  void insert(pnode &p, pnode it) {
    if (!p)
      p = it;
    else if (it->prior > p->prior) {
      split(p, it->key, it->l, it->r);
      p = it;
    } else {
      if (it->key < p->key)
        insert(p->l, it);
      else
        insert(p->r, it);
    }
    update(p);
  }
  void merge(pnode &p, pnode l, pnode r) {
    if (!l || !r) {
      if (l)
        p = l;
      else
        p = r;
    } else if (l->prior > r->prior) {
      merge(l->r, l->r, r);
      p = l;
    } else {
      merge(r->l, l, r->l);
      p = r;
    }
    update(p);
  }
  void erase(pnode &p, T key) {
    if (p->key == key)
      merge(p, p->l, p->r);
    else if (key < p->key)
      erase(p->l, key);
    else
      erase(p->r, key);
    update(p);
  }
};
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string second;
  cin >> second;
  for (int i = 0; i < n; i++) pos[second[i] - 'a'].push_back(i);
  reverse((second).begin(), (second).end());
  treap<int> t;
  long long res = 0;
  for (int i = n - 1; i >= 0; i--) {
    int v = second[i] - 'a';
    int p = pos[v].back();
    int ini = p;
    pos[v].pop_back();
    p -= t.order_of_key(p);
    res += i - p;
    t.insert(ini);
  }
  cout << (res) << '\n';
  return 0;
}
