#include <bits/stdc++.h>
using namespace std;
void solve() {}
const long long maxn = 300500;
long long n;
string s;
long long d = 0;
long long bad() {
  if (false) cerr << "aaaa" << '\n';
  if (false) cerr << ("s") << " is " << (s) << '\n';
  vector<vector<long long>> suf(2, vector<long long>(maxn, 0));
  for (long long i = n; i >= 1; i--) {
    suf[s[i] - 'A'][i]++;
  }
  for (long long i = 0; i <= 1; i++) {
    for (long long j = n; j >= 1; j--) {
      suf[i][j] += suf[i][j + 1];
    }
  }
  long long res = 0;
  for (long long i = 1; i <= n; i++) {
    long long cur = s[i] - 'A';
    char op;
    if (s[i] == 'A')
      op = 'B';
    else
      op = 'A';
    if (suf[cur ^ 1][i + 1]) {
      if (i < n and s[i + 1] == op) {
        if (false) cerr << "double count on " << i << '\n';
        d++;
      }
      res++;
    }
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  cin >> s;
  s = "." + s;
  long long ans = ((n - 1) * n) / 2;
  ans -= bad();
  reverse((s).begin(), (s).end());
  s.pop_back();
  s = "." + s;
  ans -= bad();
  if (false) cerr << ("d") << " is " << (d) << '\n';
  ans += d / 2;
  cout << ans << '\n';
}
