#include <bits/stdc++.h>
using namespace std;
vector<long long> itv, lz;
long long E;
long long itr(long long x1, long long y1, long long x2, long long y2) {
  if (x1 > y2 || x2 > y1) return 0;
  if (x1 <= x2 && y2 <= y1) return 2;
  return 1;
}
void up(long long val, long long s1, long long e1, long long s = 0,
        long long e = E, long long p = 1) {
  if (lz[p] != 0) {
    itv[p] += lz[p];
    if (s != e) {
      lz[2 * p] += lz[p];
      lz[2 * p + 1] += lz[p];
    }
    lz[p] = 0;
  }
  long long res = itr(s1, e1, s, e);
  if (res == 0) return;
  if (s == e) {
    itv[p] += val;
    return;
  }
  if (res == 2) {
    itv[p] += val;
    lz[2 * p] += val;
    lz[2 * p + 1] += val;
    return;
  }
  long long mid = ((s) + (e)) / 2;
  ;
  up(val, s1, e1, s, mid, 2 * p);
  up(val, s1, e1, mid + 1, e, 2 * p + 1);
  itv[p] = itv[2 * p] + itv[2 * p + 1];
}
long long query(long long s1, long long e1, long long s = 0, long long e = E,
                long long p = 1) {
  if (lz[p] != 0) {
    itv[p] += lz[p];
    if (s != e) {
      lz[2 * p] += lz[p];
      lz[2 * p + 1] += lz[p];
    }
    lz[p] = 0;
  }
  long long res = itr(s1, e1, s, e);
  if (res == 0) return 0;
  if (res == 2) return itv[p];
  long long mid = ((s) + (e)) / 2;
  ;
  return query(s1, e1, s, mid, 2 * p) + query(s1, e1, mid + 1, e, 2 * p + 1);
}
vector<vector<long long>> v;
long long ind[30];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  string t = s;
  reverse(t.begin(), t.end());
  E = n - 1;
  (itv) = vector<long long>(1 << (long long)(1 + ceil(log2((n)))));
  (lz) = vector<long long>(1 << (long long)(1 + ceil(log2((n)))));
  (v) = vector<vector<long long>>((30) + 2);
  for (long long i = 0; i < n; i++) v[s[i] - 'a'].push_back(i);
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    char c = t[i];
    long long index = v[c - 'a'][ind[c - 'a']];
    ind[c - 'a']++;
    long long shift = query(index, index);
    ans += shift + index - i;
    up(1, 0, index);
  }
  cout << ans;
  return 0;
}
