#include <bits/stdc++.h>
#pragma optimize(2)
using namespace std;
const long long N = 200010;
const long long mod = 10000;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    qq;
long long dx[] = {1, -1, 0, 0};
long long dy[] = {0, 0, 1, -1};
inline long long read() {
  long long x = 0;
  bool f = 0;
  char ch = getchar();
  while (ch < '0' || '9' < ch) f |= ch == '-', ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
void out(long long n) {
  if (n > 9) out(n / 10);
  putchar(n % 10 + '0');
}
long long n, m, res, T, cnt;
long long c[N];
queue<long long> q[50];
char s[N];
long long lowbit(long long x) { return x & (-x); }
void update(long long x, long long y) {
  while (x <= n) {
    c[x] += y;
    x += lowbit(x);
  }
  return;
}
long long query(long long x) {
  long long res = 0;
  while (x) {
    res += c[x];
    x -= lowbit(x);
  }
  return res;
}
int main() {
  scanf("%lld", &n);
  cin >> (s + 1);
  for (long long i = 1; i <= n; i++) {
    q[s[i] - 'a'].push(i);
    update(i, 1);
  }
  for (long long i = n; i >= 1; i--) {
    long long tmp = q[s[i] - 'a'].front();
    q[s[i] - 'a'].pop();
    res += query(tmp) - 1;
    update(tmp, -1);
  }
  printf("%lld\n", res);
  return 0;
}
