#include <bits/stdc++.h>
using namespace std;
long long _mergeSort(int arr[], int temp[], int left, int right);
long long merge(int arr[], int temp[], int left, int mid, int right);
long long mergeSort(int arr[], int array_size) {
  int temp[array_size];
  return _mergeSort(arr, temp, 0, array_size - 1);
}
long long _mergeSort(int arr[], int temp[], int left, int 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 merge(int arr[], int temp[], int left, int mid, int right) {
  int i, j, k;
  long long inv_count = 0;
  i = left;
  j = mid;
  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;
}
vector<int> in[30];
vector<int> fi[30];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    string s1;
    cin >> s1;
    string s2 = s1;
    reverse(s2.begin(), s2.end());
    for (int i = 0; i < n; i++) {
      in[s1[i] - 'a'].push_back(i);
      fi[s2[i] - 'a'].push_back(i);
    }
    int arr[n];
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < (int)fi[i].size(); j++) {
        arr[fi[i][j]] = in[i][j];
      }
    }
    long long ans = mergeSort(arr, n);
    cout << ans << endl;
  }
  return 0;
}
