#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<long long> pos[30];
class fen {
 public:
  const long long dim = 1 << 19;
  vector<long long> v = vector<long long>(dim + 1);
  void upd(long long pos) {
    for (long long i = pos; i <= dim; i += (i & (-i))) v[i]++;
  }
  long long query(long long pos) {
    long long t = 0;
    for (long long i = pos; i > 0; i -= (i & (-i))) t += v[i];
    return t;
  }
  long long q(long long k) { return v[dim] - query(k); }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long N;
  cin >> N;
  string a, t;
  cin >> a;
  t = a;
  for (long long i = 0; i < N; i++) {
    pos[a[i] - 'a'].push_back(i);
  }
  for (long long i = 0; i < 28; i++) reverse(pos[i].begin(), pos[i].end());
  reverse(t.begin(), t.end());
  fen k;
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    long long y = t[i] - 'a';
    long long cur = pos[y].back();
    pos[y].pop_back();
    k.upd(cur + 1);
    cur += k.q(cur + 1);
    ans += abs(cur - i);
  }
  cout << ans;
}
