#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
template <typename T>
using mxpq = priority_queue<T>;
template <typename T>
using mnpq = priority_queue<T, vector<T>, greater<T>>;
const long long mod = 1e9 + 7;
const long long N = 1e6 + 6;
const long long maxN = 2e5 + 15;
const long long MAX_SIZE = 2e6 + 6;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const double PI = 3.14159265359;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
long long powerM(long long x, long long y, long long M = mod) {
  long long v = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1) v = (v * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return v;
}
long long power(long long x, long long y) {
  long long v = 1;
  while (y > 0) {
    if (y & 1) v = v * x;
    y = y >> 1;
    x = x * x;
  }
  return v;
}
int n;
int trie[10 + (1 << 19)][2];
void upd(int val) {
  int ptr = 0;
  for (int i = 18 - 1; i >= 0; --i) {
    long long bit = (val & (1 << i) ? 1 : 0);
    if (bit == 1) {
      trie[ptr][1]++;
      ptr = ptr * 2 + 2;
    } else {
      trie[ptr][0]++;
      ptr = 2 * ptr + 1;
    }
  }
}
long long sum(int val) {
  int ptr = 0;
  long long inv = 0;
  for (int i = 18 - 1; i >= 0; --i) {
    int bit = (val & (1 << i) ? 1 : 0);
    if (bit == 1) {
      inv += trie[ptr][0];
      ptr = ptr * 2 + 2;
    } else {
      ptr = 2 * ptr + 1;
    }
  }
  return inv;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> n >> s;
  map<char, vector<int>> mp;
  for (int i = 0; i < n; ++i) {
    mp[s[i]].push_back(i);
  }
  long long ans = 0;
  reverse(s.begin(), s.end());
  for (int i = n - 1; i >= 0; --i) {
    char ch = s[i];
    int op = mp[ch].back();
    mp[ch].pop_back();
    ans += i - (op - sum(op));
    upd(op);
  }
  cout << ans;
  return 0;
}
