#include <bits/stdc++.h>
using namespace std;
class fente {
 public:
  long long n;
  long long *bit;
  fente(long long a) {
    n = a;
    bit = new long long[a + 1];
    for (long long i = 0; i <= a; i++) bit[i] = 0;
  }
  void update(long long idx, long long inc) {
    for (long long i = idx; i <= n; i += (i & (-i))) bit[i] += inc;
  }
  long long query(long long idx) {
    long long sum = 0;
    for (long long i = idx; i >= 1; i -= (i & (-i))) sum += bit[i];
    return sum;
  }
};
void solve() {
  long long n;
  cin >> n;
  string second;
  cin >> second;
  vector<long long> v[26];
  for (long long i = 0; i < (long long)(second).size(); i++) {
    v[second[i] - 'a'].push_back(i);
  }
  vector<long long> nv[26];
  long long b[n];
  reverse(second.begin(), second.end());
  for (long long i = 0; i < (long long)(second).size(); i++) {
    nv[second[i] - 'a'].push_back(i);
  }
  map<long long, long long> m;
  for (long long i = 0; i < 26; i++) {
    for (long long j = 0; j < (long long)(v[i]).size(); j++)
      b[nv[i][j]] = v[i][j] + 1;
  }
  fente first(n + 1);
  long long ans = 0;
  for (long long i = n - 1; i >= 0; i--) {
    ans += first.query(b[i]);
    first.update(b[i], 1);
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
