#include <bits/stdc++.h>
using namespace std;
long long merge(long long a[], long long l, long long mid, long long r) {
  long long cnt = 0;
  long long size_l = mid - l + 1, size_r = r - mid;
  long long left[size_l], right[size_r];
  for (long long i = 0; i < size_l; i++) left[i] = a[l + i];
  for (long long i = 0; i < size_r; i++) right[i] = a[mid + i + 1];
  long long i = 0, j = 0, k = l;
  while (i < size_l && j < size_r) {
    if (left[i] <= right[j]) {
      a[k++] = left[i++];
    } else {
      a[k++] = right[j++];
      cnt += size_l - i;
    }
  }
  while (i < size_l) {
    a[k++] = left[i++];
  }
  while (j < size_r) {
    a[k++] = right[j++];
  }
  return cnt;
}
long long mergeSort(long long a[], long long l, long long r) {
  long long ans = 0;
  long long mid;
  if (r > l) {
    mid = (l + r) / 2;
    ans += mergeSort(a, l, mid);
    ans += mergeSort(a, mid + 1, r);
    ans += merge(a, l, mid, r);
  }
  return ans;
}
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  vector<queue<long long>> pos(30);
  string tmp = s;
  reverse((tmp).begin(), (tmp).end());
  for (long long i = 0; i < n; i++) {
    long long x = tmp[i] - 'a';
    pos[x].push(i);
  }
  long long a[n];
  for (long long i = 0; i < n; i++) {
    long long x = s[i] - 'a';
    a[i] = pos[x].front();
    pos[x].pop();
  }
  cout << mergeSort(a, 0, n - 1) << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
