#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long seg[4 * 500001], a[500001];
void built(long long l, long long r, long long treei) {
  if (l == r) {
    seg[treei] = a[l];
    return;
  }
  long long m = (l + r) / 2;
  built(l, m, 2 * treei);
  built(m + 1, r, 2 * treei + 1);
  seg[treei] = min(seg[2 * treei], seg[2 * treei + 1]);
}
void update(long long l, long long r, long long i, long long value,
            long long treei) {
  if (l == r) {
    seg[treei] = value;
    return;
  }
  long long m = (l + r) / 2;
  if (i <= m)
    update(l, m, i, value, 2 * treei);
  else
    update(m + 1, r, i, value, 2 * treei + 1);
  seg[treei] = seg[2 * treei] + seg[2 * treei + 1];
}
long long query(long long l, long long r, long long s, long long e,
                long long treei) {
  if (s > r || e < l) return 0LL;
  if (s >= l && e <= r) return seg[treei];
  long long m = (s + e) / 2;
  long long left = query(l, r, s, m, 2 * treei);
  long long right = query(l, r, m + 1, e, 2 * treei + 1);
  return left + right;
}
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  for (long long i = 0; i < n + 1; ++i) a[i] = 0;
  for (long long i = 0; i < 500001 * 4; ++i) seg[i] = 0;
  built(0LL, n - 1, 1LL);
  string hi = s;
  reverse(hi.begin(), hi.end());
  std::vector<long long> v[26];
  int index[26];
  for (long long i = 0; i < 26; ++i) index[i] = 0;
  for (long long i = 0; i < n; ++i) {
    v[s[i] - 'a'].push_back(i);
  }
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    long long ind = index[hi[i] - 'a'];
    long long ye = v[hi[i] - 'a'][ind];
    index[hi[i] - 'a']++;
    long long done = query(0, ye, 0, n - 1, 1LL);
    ans += max(0LL, ye - done);
    update(0LL, n - 1, ye, 1LL, 1LL);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
  cerr << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
