#include <bits/stdc++.h>
using namespace std;
const int SIGMA = 26;
struct Fenwick {
  int n;
  vector<int> t;
  Fenwick(int n_) : n(n_), t(n + 1) {}
  inline int lsb(int x) { return x & -x; }
  void Update(int pos, int val) {
    for (++pos; pos <= n; pos += lsb(pos)) t[pos] += val;
  }
  int Get(int pos) {
    int ans = 0;
    for (++pos; pos; pos -= lsb(pos)) ans += t[pos];
    return ans;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<int>> pos(SIGMA);
  for (int i = n - 1; i >= 0; --i) {
    pos[s[i] - 'a'].emplace_back(i);
  }
  int64_t ans = 0;
  Fenwick fw(n);
  for (int i = 0; i < n; ++i) {
    int c = s[n - i - 1] - 'a';
    int p = pos[c].back();
    pos[c].pop_back();
    ans += p + fw.Get(p);
    fw.Update(p, -1);
  }
  cout << ans << endl;
}
