#include <bits/stdc++.h>
using namespace std;
int n;
string s;
vector<int> a[26];
int b[200010];
long long ans;
int t[200010];
inline void add(int i, int v) {
  for (; i < n; i = i | (i + 1)) t[i] += v;
}
inline int get(int i) {
  int ans = 0;
  for (; i >= 0; i = (i & (i + 1)) - 1) ans += t[i];
  return ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; ++i) {
    a[s[i] - 'a'].push_back(i);
  }
  for (auto i : a) {
    for (int j = 0; j < i.size(); ++j) {
      b[i[j]] = n - i[i.size() - j - 1] - 1;
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    ans += get(b[i]);
    add(b[i], 1);
  }
  cout << ans;
  return 0;
}
