#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 1000;
const long long INF64 = 1e18 + 1000;
const double EPS = 1e-9;
const int N = 2e6 + 200;
const int MOD = 1e9 + 7;
int add(int x, int y, int m = MOD) {
  x += y;
  while (x >= m) x -= m;
  while (x < 0) x += m;
  return x;
}
int sub(int x, int y, int m = MOD) { return add(x, -y, m); }
int mul(long long x, long long y, int m = MOD) {
  x %= m;
  y %= m;
  return (x * 1ll * y) % m;
}
int binpow(int x, int y, int m = MOD) {
  int z = 1;
  while (y) {
    if (y & 1) z = mul(z, x, m);
    x = mul(x, x, m);
    y >>= 1;
  }
  return z;
}
int divide(int x, int y, int m = MOD) { return mul(x, binpow(y, m - 2, m), m); }
int T[4 * N];
void build(int v, int l, int r) {
  if (r - l == 1) {
    T[v] = 1;
    return;
  }
  int m = (r + l) / 2;
  build(2 * v + 1, l, m);
  build(2 * v + 2, m, r);
  T[v] = T[2 * v + 1] + T[2 * v + 2];
}
int get(int v, int tl, int tr, int l, int r) {
  if (l >= r) {
    return 0;
  }
  if (tl == l && tr == r) {
    return T[v];
  }
  int tm = (tl + tr) / 2;
  return get(2 * v + 1, tl, tm, l, min(r, tm)) +
         get(2 * v + 2, tm, tr, max(l, tm), r);
}
void upd(int v, int l, int r, int i) {
  if (r - l == 1) {
    T[v] = 0;
    return;
  }
  int m = (r + l) / 2;
  if (i < m) {
    upd(2 * v + 1, l, m, i);
  } else {
    upd(2 * v + 2, m, r, i);
  }
  T[v] = T[2 * v + 1] + T[2 * v + 2];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  string rev = s;
  reverse(rev.begin(), rev.end());
  set<int> cont[26];
  for (int i = 0; i < n; i++) {
    cont[s[i] - 'a'].insert(i);
  }
  long long ans = 0;
  int l = 0, r = 0;
  build(0, 0, n);
  vector<int> used(n);
  while (r < n) {
    while (used[l]) ++l;
    if (s[l] == rev[r]) {
      upd(0, 0, n, l);
      cont[rev[r] - 'a'].erase(l);
      r++;
      l++;
    } else {
      int left = l;
      int right = *cont[rev[r] - 'a'].upper_bound(l);
      cont[rev[r] - 'a'].erase(right);
      ans += get(0, 0, n, left, right + 1) - 1;
      upd(0, 0, n, right);
      used[right] = 1;
      r++;
    }
  }
  cout << ans;
}
