#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
vector<int> seg;
long long ans = 0;
long long expo(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  long long z = expo(a, b / 2);
  z *= z;
  z %= 1000000007;
  if (b % 2 != 0) {
    z *= a;
    z %= 1000000007;
  }
  return z;
}
void Merge(int l, int mid, int r) {
  int len1 = mid - l + 1;
  int len2 = r - mid;
  int A[len1 + 1];
  int B[len2 + 1];
  for (int i = 0; i < len1; i++) {
    A[i] = arr[l + i];
  }
  A[len1] = 100000000;
  for (int i = 0; i < len2; i++) {
    B[i] = arr[mid + 1 + i];
  }
  B[len2] = 100000000;
  int a = 0;
  int b = 0;
  for (int i = 0; i < (len1 + len2); i++) {
    if (A[a] > B[b]) {
      long long ans1 = len1 - a;
      ans += ans1;
      arr[l + i] = B[b];
      b++;
    } else {
      arr[l + i] = A[a];
      a++;
    }
  }
}
void Partition(int l, int r) {
  if (l >= r) {
    return;
  }
  int mid = l + (r - l) / 2;
  Partition(l, mid);
  Partition(mid + 1, r);
  Merge(l, mid, r);
}
long long query(int node, int s, int e, int l, int r) {
  if (s > r || e < l) {
    return 0;
  }
  if (s >= l && e <= r) {
    return seg[node];
  }
  int mid = (s + e) / 2;
  long long ans1 =
      query(2 * node + 1, s, mid, l, r) + query(2 * node + 2, mid + 1, e, l, r);
  return ans1;
}
void update(int node, int l, int r, int val) {
  if (l > r) {
    return;
  }
  if (l == r) {
    seg[node] = 1;
    return;
  }
  int mid = (l + r) / 2;
  if (val <= mid) {
    update(2 * node + 1, l, mid, val);
  } else {
    update(2 * node + 2, mid + 1, r, val);
  }
  seg[node] = seg[2 * node + 1] + seg[2 * node + 2];
}
void solve() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  set<int> v[30];
  arr.clear();
  arr.resize(n + 2, 0);
  for (int i = 0; i < n; i++) {
    int x = str[i] - 'a';
    v[x].insert(i);
  }
  seg.clear();
  seg.resize(4 * n + 5, 0);
  int i = 0;
  int j = n - 1;
  int curr = i;
  while (i < n) {
    int x = str[j] - 'a';
    int z;
    for (auto &hk : v[x]) {
      if (arr[hk] == 0) {
        z = hk;
        v[x].erase(hk);
        break;
      }
    }
    arr[z] = i + 1;
    i++;
    j--;
  }
  ans = 0;
  for (int i = 0; i < n; i++) {
    long long ans1 = query(0, 0, n, arr[i] + 1, n);
    ans += ans1;
    update(0, 0, n, arr[i]);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
