#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
const long long MOD = 1e9 + 7;
const long long INF = LLONG_MAX;
const long long SIZE = 100000;
vector<long long> temp(200005), v;
long long merge(long long l, long long mid, long long r) {
  long long cnt = 0;
  long long k = l, i = l, j = mid;
  while (i < mid && j <= r) {
    if (v[i] < v[j]) {
      temp[k] = v[i];
      i++;
      k++;
    } else {
      cnt += mid - i;
      temp[k] = v[j];
      j++;
      k++;
    }
  }
  while (i < mid) {
    temp[k] = v[i];
    i++;
    k++;
  }
  while (j <= r) {
    temp[k] = v[j];
    j++;
    k++;
  }
  for (long long i = l; i < (long long)r + 1; i++) v[i] = temp[i];
  return cnt;
}
long long mergesort(long long l, long long r) {
  long long cnt = 0;
  if (r > l) {
    long long mid = (l + r) / 2;
    cnt += mergesort(l, mid);
    cnt += mergesort(mid + 1, r);
    cnt += merge(l, mid + 1, r);
  }
  return cnt;
}
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  map<char, queue<long long>> make_pair;
  for (long long i = 0; i < (long long)n; i++) {
    make_pair[s[i]].push(i + 1);
  }
  reverse(s.begin(), s.end());
  v.clear();
  for (long long i = 0; i < (long long)n; i++) {
    v.push_back(make_pair[s[i]].front());
    make_pair[s[i]].pop();
  }
  long long ans = mergesort(0, n - 1);
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
