#include <bits/stdc++.h>
using namespace std;
long long int tc, n, m, k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  vector<long long> tree(n + 1, 0);
  auto update = [&](long long int i, long long int delta) {
    while (i <= n) {
      tree[i] += delta;
      i += i & (-i);
    }
  };
  auto query = [&](long long int i) {
    long long int sum = 0;
    while (i > 0) {
      sum += tree[i];
      i -= i & (-i);
    }
    return sum;
  };
  string str;
  cin >> str;
  vector<vector<long long> > hm(26);
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    auto ind = str[i] - 'a';
    hm[ind].push_back(i + 1);
  }
  vector<long long> pos(n, 0);
  for (__typeof(26) i = (0) - ((0) > (26)); i != (26) - ((0) > (26));
       i += 1 - 2 * ((0) > (26))) {
    vector<long long> vec = hm[i];
    for (__typeof(((int)(vec).size())) j = (0) - ((0) > (((int)(vec).size())));
         j != (((int)(vec).size())) - ((0) > (((int)(vec).size())));
         j += 1 - 2 * ((0) > (((int)(vec).size())))) {
      vec[j] = n + 1 - vec[j];
    }
    sort(vec.begin(), vec.end());
    for (__typeof(((int)(hm[i]).size())) j =
             (0) - ((0) > (((int)(hm[i]).size())));
         j != (((int)(hm[i]).size())) - ((0) > (((int)(hm[i]).size())));
         j += 1 - 2 * ((0) > (((int)(hm[i]).size())))) {
      pos[hm[i][j] - 1] = vec[j];
    }
  }
  long long int inv_count = 0;
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    inv_count += (query(n) - query(pos[i]));
    update(pos[i], 1);
  }
  cout << inv_count;
}
