#include <bits/stdc++.h>
using namespace std;
long long merge(vector<int> &arr, int l, int m, int r) {
  vector<int> aux(r - l + 1);
  int i = l, j = m + 1, k = 0;
  long long inversions = 0;
  while (i <= m && j <= r) {
    if (arr[i] < arr[j])
      aux[k++] = arr[i++];
    else {
      aux[k++] = arr[j++];
      inversions += m - i + 1;
    }
  }
  while (i <= m) aux[k++] = arr[i++];
  while (j <= r) aux[k++] = arr[j++];
  for (i = l, k = 0; i <= r; i++, k++) arr[i] = aux[k];
  return inversions;
}
long long merge_sort(vector<int> &arr, int l, int r) {
  if (l == r) return 0;
  int m = (l + r) / 2;
  long long inversions_left = merge_sort(arr, l, m);
  long long inversions_right = merge_sort(arr, m + 1, r);
  return inversions_left + inversions_right + merge(arr, l, m, r);
}
long long count_inversions(vector<int> arr) {
  return merge_sort(arr, 0, arr.size() - 1);
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  unordered_map<char, stack<int>> m;
  for (int i = 0; i < n; i++) {
    char c = s[i];
    m[c].push(n - i - 1);
  }
  vector<int> perm;
  for (int i = 0; i < n; i++) {
    char c = s[i];
    int pos = m[s[i]].top();
    m[s[i]].pop();
    perm.push_back(pos);
  }
  cout << count_inversions(perm) << endl;
}
