#include <bits/stdc++.h>
using namespace ::std;
int n;
string s;
int arr[200001];
vector<int> v[26];
int cnt[26];
long long int binary[200001];
long long int ans;
long long int sum(int a) {
  long long int x = a + 1;
  long long int sm = 0;
  while (x > 0) {
    sm += binary[x];
    x -= (x & (-x));
  }
  return sm;
}
void update(int a, int value) {
  long long int x = a + 1;
  while (x <= n) {
    binary[x] += value;
    x += (x & (-x));
  }
  return;
}
void solve() {
  for (int i = n - 1; i >= 0; i--) {
    ans += sum(arr[i]);
    update(arr[i], 1);
  }
}
int main() {
  cin >> n;
  cin >> s;
  ans = 0;
  for (int i = 0; i < n; i++) {
    int a = s[i] - 'a';
    v[a].push_back(i);
  }
  for (int i = n - 1; i >= 0; i--) {
    int a = s[i] - 'a';
    arr[n - 1 - i] = v[a][cnt[a]];
    cnt[a]++;
  }
  solve();
  cout << ans << endl;
}
