#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long INF = 1LL << 60;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class Abel>
struct BIT {
 private:
  vector<Abel> node;
  long long n;
  Abel UNITY_SUM = 0;

 public:
  BIT(long long n_) {
    n = n_;
    node.resize(n, UNITY_SUM);
  }
  void add(long long a, Abel w) {
    for (long long i = a; i < n; i |= i + 1) node[i] += w;
  }
  Abel sum(long long a) {
    Abel ret = UNITY_SUM;
    for (long long i = a - 1; i >= 0; i = (i & (i + 1)) - 1) ret += node[i];
    return ret;
  }
  Abel sum(long long a, long long b) { return sum(b) - sum(a); }
  long long get(long long k) {
    ++k;
    long long res = 0;
    long long n = 1;
    while (n < (long long)node.size()) n *= 2;
    for (long long i = n / 2; i > 0; i /= 2) {
      if (res + i <= (long long)node.size() && node[res + i - 1] < k) {
        k -= node[res + i - 1];
        res += i;
      }
    }
    return res;
  }
  void print() {
    for (long long i = 0; i < n; ++i) cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};
void solve() {
  long long n;
  string s;
  cin >> n >> s;
  string t = s;
  reverse(t.begin(), t.end());
  const long long N = 26;
  vector<deque<long long>> g(N);
  for (long long i = 0; i < n; ++i) {
    long long c = s[i] - 'a';
    g[c].push_back(i);
  }
  vector<long long> res(n, -1);
  for (long long i = 0; i < n; ++i) {
    long long c = t[i] - 'a';
    res[i] = g[c].front();
    g[c].pop_front();
  }
  long long ans = 0;
  BIT<long long> bit(n);
  for (long long i = 0; i < n; ++i) {
    ans += bit.sum(res[i], n);
    bit.add(res[i], 1);
  }
  cout << ans << "\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
