#include <bits/stdc++.h>
using namespace std;
void setIO(const string &name = "") {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  if (name.length()) {
    freopen((name + ".in").c_str(), "r", stdin);
    freopen((name + ".out").c_str(), "w", stdout);
  }
}
template <typename T>
void read(vector<T> &a) {
  for (auto &x : a) cin >> x;
}
template <typename T>
void read(vector<T> &a, long long n) {
  a.resize(n);
  for (auto &x : a) cin >> x;
}
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &v) {
  out << "(";
  out << v.first << "," << v.second;
  return out << ")";
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  for (auto(i) = 0; (i) < ((long long)(v).size()); (i)++) {
    if (i) out << ", ";
    out << v[i];
  }
  return out << "]";
}
template <typename T>
void print(vector<T> &a) {
  for (const auto &x : a) cout << x << ' ';
  cout << '\n';
}
void MOD(long long &x, long long m = 1000000007) {
  x %= m;
  if (x < 0) x += m;
}
template <typename T>
void dbg(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename T, typename... U>
void dbg(const char *names, T &&arg1, U &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  dbg(comma + 1, args...);
}
template <class T>
void read(T &x) {
  cin >> x;
}
template <class T, class... U>
void read(T &t, U &...u) {
  read(t);
  read(u...);
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
struct SegTree {
  long long size = 1;
  vector<long long> elements, lazy;
  void init(long long n) {
    while (size < n) size *= 2;
    elements.assign(2 * size, 0);
    lazy.assign(2 * size, 0);
  }
  void pull(long long x) {
    elements[x] = elements[2 * x + 1] + elements[2 * x + 2];
  }
  void build(vector<long long> &a, long long x, long long lx, long long rx) {
    if (rx - lx == 1) {
      if (lx < (long long)(a).size()) {
        elements[x] = a[lx];
      }
      return;
    }
    long long mid = (lx + rx) / 2;
    build(a, 2 * x + 1, lx, mid);
    build(a, 2 * x + 2, mid, rx);
    pull(x);
  }
  void build(vector<long long> &a) { build(a, 0, 0, size); }
  void push(long long x, long long lx, long long rx) {
    long long to_add = lazy[x] * (rx - lx) / 2;
    elements[2 * x + 1] += to_add;
    elements[2 * x + 2] += to_add;
    lazy[2 * x + 1] += lazy[x];
    lazy[2 * x + 2] += lazy[x];
    lazy[x] = 0;
  }
  long long calc(long long l, long long r, long long x, long long lx,
                 long long rx) {
    if (l >= rx or r <= lx) return 0;
    if (l <= lx and rx <= r) {
      return elements[x];
    }
    push(x, lx, rx);
    long long mid = (lx + rx) / 2;
    long long r1 = calc(l, r, 2 * x + 1, lx, mid);
    long long r2 = calc(l, r, 2 * x + 2, mid, rx);
    pull(x);
    return r1 + r2;
  }
  long long calc(long long l, long long r) { return calc(l, r, 0, 0, size); }
  void add(long long l, long long r, long long v, long long x, long long lx,
           long long rx) {
    if (l >= rx or r <= lx) return;
    if (l <= lx and rx <= r) {
      lazy[x] += v;
      elements[x] += v * (rx - lx);
      return;
    }
    push(x, lx, rx);
    long long mid = (lx + rx) / 2;
    add(l, r, v, 2 * x + 1, lx, mid);
    add(l, r, v, 2 * x + 2, mid, rx);
    pull(x);
  }
  void add(long long l, long long r, long long v) { add(l, r, v, 0, 0, size); }
};
long long n;
string s, t;
vector<vector<long long> > pos;
vector<long long> idx;
SegTree st;
long long ans = 0;
void init() {
  read(n, s);
  pos.resize(26);
  idx.assign(26, 0);
  for (auto(i) = 0; (i) < (n); (i)++) {
    pos[s[i] - 'a'].emplace_back(i);
  }
  st.init(n);
  t = s;
  reverse((t).begin(), (t).end());
}
int32_t main() {
  setIO();
  init();
  long long x;
  for (auto(i) = 0; (i) < (n); (i)++) {
    x = t[i] - 'a';
    assert(idx[x] < (long long)(pos[x]).size());
    long long p = pos[x][idx[x]];
    if (p < n - 1) st.add(p + 1, n, -1);
    p += st.calc(p, p + 1);
    ans += p;
    idx[x]++;
  }
  cout << ans << '\n';
  return 0;
}
