#include <bits/stdc++.h>
using namespace std;
inline int lowbit(int x) { return x & (-x); }
const int N = 2e5 + 7;
int q[N], tmp[N];
int mp[26][N], cnt[26];
int used[26];
char a[N];
long long merge(int l, int r) {
  if (l >= r) return 0;
  int mid = l + r >> 1;
  long long res = merge(l, mid) + merge(mid + 1, r);
  int k = 0, i = l, j = mid + 1;
  while (i <= mid && j <= r) {
    if (q[i] <= q[j])
      tmp[k++] = q[i++];
    else {
      res += mid - i + 1;
      tmp[k++] = q[j++];
    }
  }
  while (i <= mid) tmp[k++] = q[i++];
  while (j <= r) tmp[k++] = q[j++];
  for (int i = l, j = 0; i <= r; ++i, ++j) q[i] = tmp[j];
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, t, k = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    char c;
    cin >> c;
    a[i] = c;
    t = c - 'a';
    mp[t][cnt[t]++] = i;
  }
  reverse(a, a + n);
  for (int i = 0; i < n; ++i) {
    int t = a[i] - 'a';
    q[k++] = mp[t][used[t]++];
  }
  cout << merge(0, n - 1) << endl;
  return 0;
}
