#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const double eps = 1e-7;
const int maxn = 2e5 + 9;
const double pi = acos(-1.0);
const double inf = 1e50;
const long long mod = 10007;
int op[maxn], n;
void add(int i, int x) {
  while (i <= n) {
    op[i] += x;
    i += i & -i;
  }
}
int sum(int i) {
  int ans = 0;
  while (i) {
    ans += op[i];
    i -= i & -i;
  }
  return ans;
}
char pq[maxn];
vector<int> io[30];
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> pq;
  for (int i = 1; i <= n; ++i) {
    add(i, 1);
    io[pq[i - 1] - 'a'].push_back(i);
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    int tmp = pq[i - 1] - 'a';
    int jk = io[tmp][io[tmp].size() - 1];
    io[tmp].pop_back();
    int pos = sum(jk);
    ans += n + 1 - i - pos;
    add(jk, -1);
  }
  cout << ans;
  return 0;
}
