#include <bits/stdc++.h>
using namespace std;
int xx[] = {0, 0, -1, 1, -1, 1, -1, 1}, yy[] = {-1, 1, 0, 0, -1, -1, 1, 1};
using ll = long long;
using ppi = pair<int, int>;
using ppl = pair<ll, ll>;
template <typename T>
using vi = vector<T>;
template <typename T>
using vvi = vector<vector<T>>;
struct Fenwick {
  static const int N = 222222;
  ll arr[N + 1];
  ll sum(int i) const {
    ll x = 0;
    while (0 < i) {
      x += arr[i];
      i &= (i - 1);
    }
    return x;
  }
  void add(int i, ll x) {
    while (i <= N) {
      arr[i] += x;
      i += (i & -i);
    }
  }
} fena, fenb;
priority_queue<int> q[26];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  string s;
  cin >> N >> s;
  for (long long i = (0); i < (N); ++i) {
    q[s[i] - 'a'].push(i + 1);
  }
  string rs = s;
  reverse(rs.begin(), rs.end());
  s = " " + s;
  rs = " " + rs;
  ll ans = 0;
  for (ll i = N; 0 < i; --i) {
    int oriIdx = q[rs[i] - 'a'].top();
    q[rs[i] - 'a'].pop();
    ll sum = 0;
    sum += fena.sum(oriIdx) * oriIdx + fenb.sum(oriIdx);
    sum -= fena.sum(oriIdx - 1) * (oriIdx - 1) + fenb.sum(oriIdx - 1);
    ll realIdx = sum + oriIdx;
    fena.add(oriIdx, -1);
    fena.add(N + 1, 1);
    fenb.add(oriIdx, (1LL - oriIdx) * -1);
    fenb.add(N + 1, 1LL * N * -1);
    ans += abs(realIdx - i);
  }
  cout << ans;
  return 0;
}
