#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long n;
class FenwickTree {
 private:
  long long n;
  vector<long long> bit;

 public:
  FenwickTree(long long n) {
    this->n = n;
    bit = vector<long long>(n, 0);
  }
  long long sum(long long r) {
    int ret = 0;
    for (; r >= 0; r = ((r & (r + 1)) - 1)) ret += bit[r];
    return ret;
  }
  int sum(long long l, long long r) { return sum(r) - sum(l - 1); }
  void add(long long index, int val) {
    for (; index < n; index = index | (index + 1)) bit[index] += val;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1, i, j, k;
  while (t--) {
    cin >> n;
    string a, b;
    cin >> a;
    b = a;
    reverse(b.begin(), b.end());
    set<long long> c[26];
    for (i = 0; i < n; i++) {
      c[a[i] - 'a'].insert(i);
    }
    FenwickTree fw(n + 1);
    long long ans = 0;
    for (i = 0; i < n; i++) {
      long long pos = (*c[b[i] - 'a'].begin());
      ans += (pos - fw.sum(pos));
      fw.add(pos, 1);
      c[b[i] - 'a'].erase(pos);
    }
    cout << ans << "\n";
  }
  return 0;
}
