#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 7;
queue<int> que[26];
long long ans = 0;
long long temp[maxn];
void Merge(long long a[], long long b[], long long l, long long m,
           long long r) {
  long long i = l, j = m + 1, k = l;
  while ((i <= m) && (j <= r))
    if (a[i] <= a[j])
      b[k++] = a[i++];
    else {
      b[k++] = a[j++];
      ans += m - i + 1;
    }
  if (i > m)
    for (long long q = j; q <= r; q++) b[k++] = a[q];
  else
    for (long long q = i; q <= m; q++) b[k++] = a[q];
  for (long long p = l; p <= r; p++) a[p] = temp[p];
}
void MergeSort(long long a[], long long left, long long right) {
  if (left >= right) {
    return;
  } else {
    long long i = (left + right) / 2;
    MergeSort(a, left, i);
    MergeSort(a, i + 1, right);
    Merge(a, temp, left, i, right);
  }
}
long long a[maxn];
void debug() {
  int n;
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  MergeSort(a, 0, n - 1);
  for (long long i = 0; i < n; ++i) {
    cout << a[i] << ' ';
  }
  cout << '\n';
  cout << ans << '\n';
}
long long ss[maxn];
int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  int len = s.length();
  for (long long i = 0; i < len; ++i) {
    que[s[i] - 'a'].push(i);
  }
  for (long long i = 0, j = n - 1; i < n / 2; ++i, j--) {
    if (s[i] != s[j]) swap(s[i], s[j]);
  }
  for (long long i = 0; i < n; ++i) {
    ss[i] = que[s[i] - 'a'].front();
    que[s[i] - 'a'].pop();
  }
  MergeSort(ss, 0, n - 1);
  cout << ans;
  return 0;
}
