#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<long long> sums;
void buildtree(int x, int lx, int rx) {
  if (lx == rx) {
    if (lx < v1.size()) sums[x] = v1[lx];
    return;
  }
  int mid = (lx + rx) / 2;
  buildtree(2 * x + 1, lx, mid);
  buildtree(2 * x + 2, mid + 1, rx);
  sums[x] = sums[2 * x + 1] + sums[2 * x + 2];
  return;
}
void update(int u, int v, int x, int l, int r) {
  if (l == r) {
    sums[x] = v;
    return;
  }
  int mid = (l + r) / 2;
  if (u <= mid)
    update(u, v, 2 * x + 1, l, mid);
  else
    update(u, v, 2 * x + 2, mid + 1, r);
  sums[x] = sums[2 * x + 1] + sums[2 * x + 2];
}
long long operate(int x, int l, int r, int lx, int rx) {
  long long ans = 0;
  if (lx <= l && rx >= r) {
    ans += sums[x];
    return ans;
  } else if (l > rx || r < lx) {
    return ans;
  } else {
    int mid = (l + r) / 2;
    ans += (operate(2 * x + 1, l, mid, lx, rx) +
            operate(2 * x + 2, mid + 1, r, lx, rx));
    return ans;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, sz = 1;
  long long ans = 0;
  cin >> n;
  v1.resize(n);
  string s, res;
  cin >> s;
  res = s;
  reverse(res.begin(), res.end());
  vector<int> va[27];
  for (int i = n - 1; i >= 0; i--) {
    va[s[i] - 96].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    v1[i] = va[res[i] - 96].back() + 1;
    va[res[i] - 96].pop_back();
  }
  while (sz < n) sz *= 2;
  sums.resize(2 * sz, 0);
  for (int i = 0; i < n; i++) {
    update(v1[i] - 1, 1, 0, 0, sz - 1);
    if (v1[i] == n)
      ans += 0;
    else
      ans += operate(0, 0, sz - 1, v1[i], n);
  }
  cout << ans << "\n";
  return 0;
}
