#include <bits/stdc++.h>
using namespace std;
const int A = 2e5 + 2;
const int mod = 1e9 + 7;
long long tree[4 * A];
void update(int node, int l, int r, int ind) {
  if (ind < l || r < ind)
    return;
  else if (l == r)
    tree[node] = 1;
  else {
    int mid = (l + r) >> 1;
    update(node << 1, l, mid, ind);
    update(node << 1 | 1, mid + 1, r, ind);
    tree[node] = tree[node << 1] + tree[node << 1 | 1];
  }
}
long long get_jump(int node, int l, int r, int low, int high) {
  if (high < l || r < low) return 0;
  if (low <= l && r <= high) return tree[node];
  long long mid = (l + r) >> 1;
  return get_jump(node << 1, l, mid, low, high) +
         get_jump(node << 1 | 1, mid + 1, r, low, high);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n;
  string s, rev;
  cin >> n >> s;
  rev = s;
  reverse(rev.begin(), rev.end());
  deque<long long> v[26];
  for (int i = 0; i < n; i++) {
    v[s[i] - 'a'].push_back(i);
  }
  long long ans = 0, acc = 0, p1 = 0;
  bool jump[n];
  memset(jump, 0, sizeof jump);
  while (p1 < n) {
    long long p2 = p1 + acc;
    if (jump[p1]) {
      acc--, p1++;
      continue;
    }
    long long ind = v[rev[p2] - 'a'].front();
    ans += ind - p1 - get_jump(1, 1, n, p1, ind);
    jump[ind] = 1;
    update(1, 1, n, ind);
    v[rev[p2] - 'a'].pop_front();
    acc++;
  }
  cout << ans << '\n';
  return 0;
}
