#include <bits/stdc++.h>
using namespace std;
void build_segment_tree(long long arr[], long long tree[], long long n) {
  for (int i = 0; i < n; i++) {
    tree[n + i] = arr[i];
  }
  for (int i = n - 1; i > 0; i--) {
    tree[i] = tree[2 * i] + tree[(2 * i) + 1];
  }
}
void update_segment_tree(int index, long long value, long long n,
                         long long tree[]) {
  tree[index + n] = value;
  index = index + n;
  for (int i = index; i >= 1; i = i / 2) {
    tree[i / 2] = tree[i] + tree[i ^ 1];
  }
}
long long query(int l, int r, long long tree[], long long n) {
  long long res = 0LL;
  for (l = l + n, r = r + n; l < r; l = l / 2, r = r / 2) {
    if (l % 2 == 1) {
      res = res + tree[l++];
    }
    if (r % 2 == 1) {
      res = res + tree[--r];
    }
  }
  return res;
}
void test() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  set<int> st[26];
  for (int i = 0; i < n; i++) st[s[i] - 'a'].insert(i);
  long long arr[n];
  for (int i = 0; i < n; i++) arr[i] = 0;
  long long tree[2 * n];
  build_segment_tree(arr, tree, n);
  long long ans = 0;
  long long cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    int x = s[i] - 'a';
    auto it = st[x].begin();
    long long y = (*it);
    long long c = query(y + 1, n, tree, n);
    ans = ans + y + c - cnt;
    update_segment_tree(y, 1, n, tree);
    st[x].erase(it);
    cnt++;
  }
  cout << ans << "\n";
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long tests = 1;
  while (tests--) {
    test();
  }
}
