#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int m) {
  if (y == 0) return 1;
  long long int p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
long long int nCr(long long int n, long long int r, long long int m) {
  if (r > n) return 0;
  long long int a = 1, b = 1, i;
  for (i = 0; i < r; i++) {
    a = (a * n) % m;
    --n;
  }
  while (r) {
    b = (b * r) % m;
    --r;
  }
  return (a * power(b, m - 2, m)) % m;
}
long long int gcd(long long int a, long long int b) {
  return (b ? gcd(b, a % b) : a);
}
class FenwickTree {
 private:
  int n;
  vector<long long int> tree, arr;

 public:
  FenwickTree(int n, vector<long long int> arr) {
    this->n = n;
    this->arr = arr;
    tree = vector<long long int>(n + 1);
    for (int i = 1; i <= n; ++i) update(i, arr[i - 1]);
  }
  void update(int index, long long int val) {
    while (index <= n) {
      tree[index] += val;
      index += index & (-index);
    }
  }
  long long int result(int index) {
    long long int Ans = 0;
    while (index > 0) {
      Ans += tree[index];
      index -= index & (-index);
    }
    return Ans;
  }
};
void solve() {
  int n;
  string s, t;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  t = s;
  reverse(s.begin(), s.end());
  vector<long long int> v(n);
  FenwickTree A(n, v);
  vector<vector<int>> last(26);
  for (int i = 0; i < n; ++i) {
    last[s[i] - 'a'].push_back(i + 1);
  }
  long long int ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    int tx = last[t[i] - 'a'].back();
    last[t[i] - 'a'].pop_back();
    ans += i - tx + 1;
    ans += A.result(tx);
    A.update(tx, 1);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
