#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = 1e18;
struct FedwickTree {
  vector<int> tree;
  int n;
  FedwickTree(int n) {
    this->n = n;
    this->tree.resize(n + 1, 0);
  }
  void insert(int pos) {
    ++pos;
    while (pos <= this->n) {
      ++this->tree[pos];
      pos += pos & -pos;
    }
  }
  int read(int pos) {
    ++pos;
    int sm = 0;
    while (pos > 0) {
      sm += this->tree[pos];
      pos -= pos & -pos;
    }
    return sm;
  }
  int read(int l, int r) { return read(r) - read(l - 1); }
};
void solve() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<queue<int>> cque(26);
  for (int i = (n)-1; i >= (0); --i) cque[str[i] - 'a'].push(n - 1 - i);
  vector<int> arr;
  for (int i = (0); i < (n); ++i) {
    arr.push_back(cque[str[i] - 'a'].front());
    cque[str[i] - 'a'].pop();
  }
  FedwickTree* ftree = new FedwickTree(n + 1);
  long long ans = 0;
  for (int i = (0); i < (n); ++i) {
    ans += ftree->read(arr[i], n);
    ftree->insert(arr[i]);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
