#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
long long bit[maxn];
void update(long long idx) {
  while (idx > 0) {
    bit[idx] += 1;
    idx -= idx & -idx;
  }
}
long long sum(long long idx) {
  long long res = 0;
  while (idx < maxn) {
    res += bit[idx];
    idx += idx & -idx;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<long long>> pos(26);
  reverse(s.begin(), s.end());
  for (long long i = 0; i < n; i++) {
    pos[s[i] - 'a'].push_back(i);
  }
  reverse(s.begin(), s.end());
  vector<long long> for_inversion;
  for (long long i = 0; i < 26; i++)
    sort(pos[i].begin(), pos[i].end(), greater<int>());
  for (long long i = 0; i < n; i++) {
    int ind = pos[s[i] - 'a'].back();
    for_inversion.push_back(ind);
    pos[s[i] - 'a'].pop_back();
  }
  long long ans = 0;
  for (long long i = 0; i < for_inversion.size(); i++) {
    ans += sum(for_inversion[i] + 1);
    update(for_inversion[i]);
  }
  cout << ans << "\n";
}
