#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5, inf = 1e18;
long long int getSum(long long int BITree[], long long int index) {
  long long int sum = 0;
  while (index > 0) {
    sum += BITree[index];
    index -= index & (-index);
  }
  return sum;
}
void updateBIT(long long int BITree[], long long int n, long long int index,
               long long int val) {
  while (index <= n) {
    BITree[index] += val;
    index += index & (-index);
  }
}
long long int getInvCount(long long int arr[], long long int n) {
  long long int invcount = 0;
  long long int maxElement = 0;
  for (long long int i = 0; i < n; i++)
    if (maxElement < arr[i]) maxElement = arr[i];
  long long int BIT[maxElement + 1];
  for (long long int i = 1; i <= maxElement; i++) BIT[i] = 0;
  for (long long int i = n - 1; i >= 0; i--) {
    invcount += getSum(BIT, arr[i] - 1);
    updateBIT(BIT, maxElement, arr[i], 1);
  }
  return invcount;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  string t = s;
  reverse(t.begin(), t.end());
  long long int ans = 0;
  long long int final[n];
  for (long long int i = 0; i < 26; ++i) {
    vector<long long int> vs, vt;
    for (long long int j = 0; j < n; ++j) {
      if (s[j] == 'a' + i) vs.push_back(j);
      if (t[j] == 'a' + i) vt.push_back(j);
    }
    for (long long int j = 0; j < ((long long int)((vs).size())); ++j) {
      final[vs[j]] = vt[j] + 1;
    }
  }
  cout << getInvCount(final, n) << "\n";
  return 0;
}
