#include <bits/stdc++.h>
using namespace std;
vector<int> bit;
vector<int> bit2;
int n;
void update(int idx, int value) {
  while (idx <= n) {
    bit[idx] += value;
    idx += (idx & (-idx));
  }
  return;
}
int query(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += bit[idx];
    idx -= (idx & (-idx));
  }
  return sum;
}
void update2(int idx, int value) {
  while (idx <= n) {
    bit2[idx] += value;
    idx += (idx & (-idx));
  }
  return;
}
int query2(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += bit2[idx];
    idx -= (idx & (-idx));
  }
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  string s;
  cin >> s;
  bit.assign(n + 1, 0);
  bit2.assign(n + 1, 0);
  vector<queue<int> > v(26);
  for (int i = 0; i < n; i++) {
    v[s[i] - 97].push(i);
  }
  reverse(s.begin(), s.end());
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    long long toplace = i;
    long long curplace = v[s[i] - 97].front();
    v[s[i] - 97].pop();
    long long ncur = curplace + query(curplace + 1);
    if (toplace < ncur) {
      sum += abs(ncur - toplace);
      update(1, 1);
      update(curplace + 2, -1);
    }
  }
  cout << sum << "\n";
}
