#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int mx = 2e5 + 5;
const long long INF = 1e18 + 3;
using namespace std;
int tr[mx], n;
vector<int> pos[26];
long long query(int i) {
  long long sum = 0;
  while (i > 0) {
    sum += tr[i];
    i -= i & (-i);
  }
  return sum;
}
void up(int i, int x) {
  while (i <= n) {
    tr[i] += x;
    i += i & (-i);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  for (int i = 0; i < 27; i++) {
    pos[i].clear();
  }
  cin >> n;
  string s = "", x = "";
  cin >> s;
  x = s;
  reverse((x).begin(), (x).end());
  for (int i = 0; i < x.size(); i++) {
    pos[x[i] - 'a'].emplace_back(i + 1);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int r = pos[s[i] - 'a'][0];
    up(r, 1);
    ans += query(n) - query(r);
    pos[s[i] - 'a'].erase(pos[s[i] - 'a'].begin());
  }
  cout << ans << "\n";
  return 0;
}
