#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int mod2 = 1e9 + 9;
inline int pow_(long long a, int n, int p = mod) {
  int r = 1;
  while (n) {
    if (n & 1) r = r * a % p;
    n >>= 1;
    a = a * a % p;
  }
  return r;
}
const int MAXALPHABET = 26;
queue<int> requiredAtPlace[MAXALPHABET];
class BIT {
 private:
  vector<int> data;
  int n;

 public:
  BIT(int n) : n(n) { data.resize(n + 1, 0); }
  int query(int x) {
    int res = 0;
    for (int iter = x; iter > 0; iter -= lastBit(iter)) {
      res += data[iter];
    }
    return res;
  }
  void add(int pos, int val) {
    for (int iter = pos; iter <= n; iter += lastBit(iter)) {
      data[iter] += val;
    }
  }
  inline int lastBit(int x) { return x & (-x); }
};
int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  string reversedS = s;
  reverse(reversedS.begin(), reversedS.end());
  int p[n];
  for (int index = 0; index < n; ++index) {
    char c = reversedS[index];
    requiredAtPlace[c - 'a'].push(index + 1);
  }
  for (int index = 0; index < n; ++index) {
    char c = s[index];
    p[index] = requiredAtPlace[c - 'a'].front();
    requiredAtPlace[c - 'a'].pop();
  }
  printf("\n");
  long long res = 0;
  BIT bit(n);
  for (int element : p) {
    res += bit.query(n) - bit.query(element - 1);
    bit.add(element, 1);
  }
  cout << res << "\n";
}
