#include <bits/stdc++.h>
using namespace std;
class Fenwick {
  int *m, *mt, N;

 public:
  Fenwick(int n);
  Fenwick(int a[], int n);
  void add_range(int r, int d);
  void add_range(int l, int r, int d);
  int sum(int r);
  int sum(int l, int r);
};
Fenwick::Fenwick(int n) {
  N = n;
  m = new int[N];
  mt = new int[N];
  memset(m, 0, sizeof(int) * N);
  memset(mt, 0, sizeof(int) * N);
}
Fenwick::Fenwick(int a[], int n) {
  N = n;
  m = new int[N];
  mt = new int[N];
  memset(m, 0, sizeof(int) * N);
  memset(mt, 0, sizeof(int) * N);
  for (int i = 0; i < N; ++i) {
    m[i] += a[i];
    if ((i | (i + 1)) < N) m[i | (i + 1)] += m[i];
  }
}
void Fenwick::add_range(int r, int d) {
  if (r < 0) return;
  for (int i = r; i >= 0; i = (i & (i + 1)) - 1) mt[i] += d;
  for (int i = r | (r + 1); i < N; i |= i + 1)
    m[i] += d * (r - (i & (i + 1)) + 1);
}
void Fenwick::add_range(int l, int r, int d) {
  add_range(r, d);
  add_range(l - 1, -d);
}
int Fenwick::sum(int r) {
  if (r < 0) return 0;
  int res = 0;
  for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
    res += m[i] + mt[i] * (i - (i & (i + 1)) + 1);
  for (int i = r | (r + 1); i < N; i |= i + 1)
    res += mt[i] * (r - (i & (i + 1)) + 1);
  return res;
}
constexpr int MAXN = 2e5 + 1;
Fenwick f(MAXN);
int Fenwick::sum(int l, int r) { return sum(r) - sum(l - 1); }
int get(int r) { return f.sum(r, r); }
void inc(int r) { f.add_range(r, 1); }
int get_pos(int c) {}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> idx[30];
  vector<int> take(30, 0);
  vector<int> prev(n);
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    idx[c].push_back(i);
    prev[i] = i - 1;
  }
  long long ans = 0;
  for (int i = n - 1, j = 0; i >= 0; --i, ++j) {
    int c = s[i] - 'a';
    auto &v = idx[c];
    int k = take[c];
    int x = v[k] + get(v[k]);
    if (x - j > 0) {
      ans += x - j;
      int p = prev[v[k]];
      if (v[k] + 1 < n) {
        prev[v[k] + 1] = p;
      }
      if (p >= 0) {
        inc(p);
      }
    }
    take[c]++;
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
