#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 101 * 1000;
string s;
int n;
vector<int> pos[26];
long long bittree[maxn];
void update(int pos, int val) {
  if (pos < n) {
    bittree[pos] += val;
    update(pos + (pos & (-pos)), val);
  }
}
long long getsum(int pos) {
  if (pos == 0) return 0;
  return getsum(pos - (pos & (-pos))) + bittree[pos];
}
int x[26];
int des[maxn];
int main() {
  cin >> n;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    pos[s[i] - 'a'].push_back(n - 1 - i);
  }
  for (int i = s.size() - 1; i >= 0; i--) {
    des[i + 1] = pos[s[i] - 'a'][x[s[i] - 'a']] + 1;
    x[s[i] - 'a']++;
  }
  long long ans = 0;
  for (int i = n; i >= 1; i--) {
    ans += getsum(des[i] - 1);
    update(des[i], 1);
  }
  cout << ans << endl;
  return 0;
}
