#include <bits/stdc++.h>
using namespace std;
long long int countInversions(long long int a[], long long int temp[],
                              long long int l, long long int r) {
  if (l >= r) {
    return 0;
  }
  long long int count1 = countInversions(a, temp, l, (l + r) / 2);
  long long int count2 = countInversions(a, temp, (l + r) / 2 + 1, r);
  long long int mergeCount = 0;
  long long int indexLeft = l;
  long long int endLeft = (l + r) / 2 + 1;
  long long int indexRight = (l + r) / 2 + 1;
  while (indexRight <= r && indexLeft < endLeft) {
    while (indexLeft < endLeft && a[indexLeft] <= a[indexRight]) {
      indexLeft++;
    }
    mergeCount += endLeft - indexLeft;
    indexRight++;
  }
  indexLeft = l, indexRight = (l + r) / 2 + 1;
  long long int idx = l;
  while (indexRight <= r && indexLeft < endLeft) {
    if (a[indexLeft] <= a[indexRight]) {
      temp[idx++] = a[indexLeft];
      indexLeft++;
    } else {
      temp[idx++] = a[indexRight];
      indexRight++;
    }
  }
  while (indexRight <= r) {
    temp[idx++] = a[indexRight];
    indexRight++;
  }
  while (indexLeft < endLeft) {
    temp[idx++] = a[indexLeft];
    indexLeft++;
  }
  for (long long int i = l; i <= r; i++) {
    a[i] = temp[i];
  }
  return mergeCount + count1 + count2;
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  map<char, deque<long long int>> positionsOfLetters;
  long long int i, n, count = 0;
  cin >> n;
  char s[n];
  for (i = 0; i < n; i++) {
    cin >> s[i];
    positionsOfLetters[s[i]].push_back(i);
  }
  reverse(s, s + n);
  long long int permuation[n];
  for (i = 0; i < n; i++) {
    permuation[i] = positionsOfLetters[s[i]].front();
    positionsOfLetters[s[i]].pop_front();
  }
  long long int temp[n + 1];
  cout << countInversions(permuation, temp, 0, n - 1);
  return 0;
}
