#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 1;
long long i, j;
long long merge(long long arr[], long long temp[], long long left,
                long long mid, long long right) {
  long long inv_count = 0;
  long long i = left;
  long long j = mid;
  long long k = left;
  while ((i <= mid - 1) && (j <= right)) {
    if (arr[i] <= arr[j])
      temp[k++] = arr[i++];
    else {
      temp[k++] = arr[j++];
      inv_count = inv_count + (mid - i);
    }
  }
  while (i <= mid - 1) temp[k++] = arr[i++];
  while (j <= right) temp[k++] = arr[j++];
  for (i = left; i <= right; i++) arr[i] = temp[i];
  return inv_count;
}
long long _mergeSort(long long arr[], long long temp[], long long left,
                     long long right) {
  long long mid, inv_count = 0;
  if (right > left) {
    mid = (right + left) / 2;
    inv_count = _mergeSort(arr, temp, left, mid);
    inv_count += _mergeSort(arr, temp, mid + 1, right);
    inv_count += merge(arr, temp, left, mid + 1, right);
  }
  return inv_count;
}
long long countSwaps(long long arr[], long long n) {
  long long temp[n];
  return _mergeSort(arr, temp, 0, n - 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  vector<long long> m[26];
  long long a[n];
  for (i = 0; i < n; i++) m[s[i] - 'a'].push_back(n - i - 1);
  for (i = 0; i < n; i++) {
    a[i] = m[s[i] - 'a'][m[s[i] - 'a'].size() - 1];
    m[s[i] - 'a'].pop_back();
  }
  cout << countSwaps(a, n);
  return 0;
}
