#include <bits/stdc++.h>
using namespace std;
long long int mpow(long long int x, long long int y);
const int MOD = 1e9 + 7, MAX = 3e5 + 5;
const long long int INF = 1e18;
struct segtree {
  int size;
  vector<long long int> tree;
  long long int dummy;
  void init(int n) {
    size = 1;
    dummy = 0;
    while (size < n) size *= 2;
    tree.assign(2 * size, dummy);
  }
  void build(vector<long long int> &a) {
    int n = a.size();
    for (long long int i = (long long int)(size); i < (long long int)(size + n);
         i++)
      tree[i] = a[i - size];
    for (long long int i = (long long int)(size - 1); i >= (long long int)(1);
         i--)
      tree[i] = tree[2 * i] + tree[2 * i + 1];
  }
  void update(int i, long long int v) {
    i += size - 1;
    tree[i] = v;
    i /= 2;
    while (i != 0) {
      tree[i] = tree[2 * i] + tree[2 * i + 1];
      i /= 2;
    }
  }
  long long int query(int l, int r, int x, int lx, int rx) {
    if (lx > r || rx < l) return dummy;
    if (lx >= l && rx <= r) return tree[x];
    int m = (lx + rx) / 2;
    return query(l, r, 2 * x, lx, m) + query(l, r, 2 * x + 1, m + 1, rx);
  }
  long long int query(int l, int r) { return query(l, r, 1, 1, size); }
};
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  string x = s;
  reverse(x.begin(), x.end());
  vector<long long int> pos[26];
  for (long long int i = (long long int)(n - 1); i >= (long long int)(0); i--) {
    pos[s[i] - 'a'].push_back(i);
  }
  segtree sgtr;
  sgtr.init(n);
  long long int ans = 0;
  for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
    long long int ind = x[i] - 'a';
    ind = pos[ind][pos[ind].size() - 1];
    ans += ind + sgtr.query(1, ind + 1);
    pos[x[i] - 'a'].pop_back();
    if (ind != n - 1) sgtr.update(ind + 2, -1);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int t = 1, i = 0;
  while (t--) {
    solve();
  }
}
long long int mpow(long long int x, long long int y) {
  if (y == 0) return 1;
  if (y % 2) return (x * mpow(x, y - 1)) % MOD;
  long long int ans = mpow(x, y / 2);
  ans = (ans * ans) % MOD;
  return ans;
}
