#include <bits/stdc++.h>
using namespace std;
long long int q, n;
long long int sum, ans;
long long int gcd(long long int a, long long int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long int powe(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      res = (res * a);
    }
    a = (a * a);
    b /= 2;
  }
  return res;
}
void the_happiest_place_on_earth() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
}
const int N = 300040;
long long int a[N], b[N], p[N], par[N], Size[N], present[N], color[N];
long long int merge(long long int a[], long long int temp[], long long int left,
                    long long int mid, long long int right) {
  long long int i, j, k;
  i = k = left;
  j = mid;
  long long int inv = 0;
  while ((i <= mid - 1) && (j <= right)) {
    if (a[i] < a[j])
      temp[k++] = a[i++];
    else {
      temp[k++] = a[j++];
      inv += (mid - i);
    }
  }
  while (i <= mid - 1) temp[k++] = a[i++];
  while (j <= right) temp[k++] = a[j++];
  for (int i = left; i <= right; i++) a[i] = temp[i];
  return inv;
}
long long int mergesort(long long int a[], long long int temp[],
                        long long int left, long long int right) {
  long long int mid, inv = 0;
  if (right > left) {
    mid = (left + right) / 2;
    inv += mergesort(a, temp, left, mid);
    inv += mergesort(a, temp, mid + 1, right);
    inv += merge(a, temp, left, mid + 1, right);
  }
  return inv;
}
void testcase() {
  cin >> n;
  string second, r;
  cin >> second;
  r = second;
  reverse(r.begin(), r.end());
  vector<long long int> v[26];
  for (int i = n - 1; i >= 0; i--) v[r[i] - 'a'].push_back(i);
  for (int i = 0; i < n; i++)
    a[i] = v[second[i] - 'a'].back() + 1, v[second[i] - 'a'].pop_back();
  cout << mergesort(a, b, 0, n - 1);
  return;
}
int main() {
  the_happiest_place_on_earth();
  the_happiest_place_on_earth();
  q = 1;
  int t = 0;
  while (q--) {
    testcase();
  }
  return 0;
}
