#include <bits/stdc++.h>
using namespace std;
vector<long long> seg_tree(900005, 0);
void update(int start, int end, int idx, int i) {
  if (start == end) {
    seg_tree[idx] = 1;
    return;
  }
  int mid = (start + end) / 2;
  if (i > mid)
    update(mid + 1, end, 2 * idx + 2, i);
  else
    update(start, mid, 2 * idx + 1, i);
  seg_tree[idx] = seg_tree[2 * idx + 1] + seg_tree[2 * idx + 2];
}
long long query(int start, int end, int l, int r, int idx) {
  if (l <= start && r >= end) return seg_tree[idx];
  if (r < start || l > end) return 0;
  int mid = (start + end) / 2;
  long long val1 = query(start, mid, l, r, 2 * idx + 1);
  long long val2 = query(mid + 1, end, l, r, 2 * idx + 2);
  return val1 + val2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string s, s1;
  cin >> s;
  s1 = s;
  reverse(s.begin(), s.end());
  vector<vector<int>> v(26);
  for (int i = 0; i < n; i++) v[s1[i] - 'a'].push_back(i + 1);
  for (int i = 0; i < 26; i++) reverse(v[i].begin(), v[i].end());
  unordered_map<int, int> ump;
  for (int i = 0; i < n; i++) {
    int m = v[s[i] - 'a'].size() - 1;
    ump[v[s[i] - 'a'][m]] = i;
    v[s[i] - 'a'].pop_back();
  }
  long long ans = 0;
  for (int i = n; i >= 1; i--) {
    ans += query(0, n - 1, 0, ump[i], 0);
    update(0, n - 1, 0, ump[i]);
  }
  cout << ans << endl;
  return 0;
}
