#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7, len = 30;
int n, lazy[N * 4], cur[27], seg[N * 4];
long long ans;
string second, t;
vector<int> vec[26];
void shift(int id) {
  lazy[id * 2] += lazy[id], lazy[id * 2 + 1] += lazy[id];
  seg[id * 2] += lazy[id], seg[id * 2 + 1] += lazy[id];
  lazy[id] = 0;
}
void upd(int x, int y, int val, int l = 0, int r = n - 1, int id = 1) {
  if (r < x || l > y) return;
  if (l >= x && r <= y) {
    lazy[id] += val;
    seg[id] += val;
    return;
  }
  int mid = (l + r) >> 1;
  shift(id);
  upd(x, y, val, l, mid, id * 2);
  upd(x, y, val, mid + 1, r, id * 2 + 1);
  seg[id] = seg[id * 2] + seg[id * 2 + 1];
}
int query(int i, int l = 0, int r = n - 1, int id = 1) {
  if (l == r) return seg[id];
  int mid = (l + r) >> 1;
  shift(id);
  if (i <= mid)
    return query(i, l, mid, id * 2);
  else
    return query(i, mid + 1, r, id * 2 + 1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie();
  cin >> n >> second;
  t = second;
  reverse(t.begin(), t.end());
  for (int i = 0; i < n; i++) vec[second[i] - 'a'].push_back(i);
  for (int i = 0; i < n; i++) {
    int pos = vec[t[i] - 'a'][cur[t[i] - 'a']++];
    ans += 1LL * abs(pos + query(pos) - i);
    upd(0, pos, 1);
  }
  cout << ans << endl;
}
