#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
}
string to_string(char c) {
  string second(1, c);
  return second;
}
template <typename T>
inline T gcd(T a, T b) {
  return a == 0 ? b : gcd(b % a, a);
}
long long int n;
string A;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> A;
  long long int ans = n * (n - 1) / 2;
  long long int p[] = {-1, -1};
  deque<long long int> dq[2];
  for (long long int i = 0; i < n; ++i) dq[A[i] - 'A'].push_back(i);
  dq[0].push_back(n), dq[1].push_back(n);
  for (long long int i = (0); i <= (long long int)(n - 1); ++i) {
    while (dq[A[i] - 'A'].size() && dq[A[i] - 'A'][0] <= i)
      dq[A[i] - 'A'].pop_front();
    ans -= i - p[A[i] - 'A'] - 1;
    ans -= dq[A[i] - 'A'][0] - i - 1;
    p[A[i] - 'A'] = i;
  }
  for (long long int i = (0); i <= (long long int)(n - 2); ++i) {
    if (A[i + 1] != A[i]) ++ans;
  }
  cout << ans << '\n';
}
