#include <bits/stdc++.h>
template <typename X>
inline X abs(const X& a) {
  return a < 0 ? -a : a;
}
template <typename X>
inline X sqr(const X& a) {
  return a * a;
}
using namespace std;
const int INF = 1000 * 1000 * 1000;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
const int N = 200 * 1000 + 13;
int n;
string s;
string revS;
vector<int> posS[30];
vector<int> posT[30];
int cnt[30];
int t[N];
inline int sum(int r) {
  int result = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r];
  return result;
}
inline void inc(int i, int d) {
  for (; i < n; i = (i | (i + 1))) t[i] += d;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
inline void read() { cin >> n >> s; }
inline void solve() {
  revS = s;
  reverse(revS.begin(), revS.end());
  for (int i = 0; i < int(s.size()); i++) {
    posS[s[i] - 'a'].push_back(i);
    posT[revS[i] - 'a'].push_back(i);
  }
  long long ans = 0;
  for (int i = 0; i < int(revS.size()); i++) {
    int let = revS[i] - 'a';
    int cur = posS[let][cnt[let]];
    int oldC = cur;
    cur += sum(cur, n - 1);
    int need = i;
    ans += cur - need;
    inc(oldC, 1);
    cnt[let]++;
  }
  cout << ans << endl;
}
int main() {
  srand(time(NULL));
  cerr << setprecision(10) << fixed;
  read();
  solve();
}
