#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 100;
char ch[N];
int a[N];
int b[N];
int n;
vector<int> g[M];
int pos[M];
long long cnt;
void merge(int l, int mid, int r) {
  int i = l, j = mid + 1;
  for (int k = l; k <= r; k++) {
    if (j > r || i <= mid && a[i] <= a[j])
      b[k] = a[i++];
    else
      b[k] = a[j++], cnt += mid - i + 1;
  }
  for (int k = l; k <= r; k++) a[k] = b[k];
}
void uqsort(int l, int r) {
  if (l < r) {
    int mid = (l + r) / 2;
    uqsort(l, mid);
    uqsort(mid + 1, r);
    merge(l, mid, r);
  }
}
void solve() {
  for (int i = 0; i < M; i++) g[i].clear();
  scanf("%d", &n);
  scanf("%s", ch);
  for (int i = 0; i < n; i++) {
    g[ch[i] - 'a'].push_back(n - i);
  }
  for (int i = 0; i < M; i++) sort(g[i].begin(), g[i].end());
  memset(pos, 0, sizeof(pos));
  for (int i = 1; i <= n; i++) {
    int j = i - 1;
    a[i] = g[ch[j] - 'a'][pos[ch[j] - 'a']];
    pos[ch[j] - 'a']++;
  }
  cnt = 0;
  uqsort(1, n);
  printf("%lld\n", cnt);
}
int main() {
  ios::sync_with_stdio(false);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
