#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 3e+5 + 5;
char s[N];
int former[N];
long long dp[N];
int main() {
  int n, fa = 0, fb = 0, a = 0, b = 0;
  long long ans = 0;
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++) {
    if (!fa && s[i] == 'A') fa = i;
    if (!fb && s[i] == 'B') fb = i;
    if (s[i] == 'A')
      former[i] = a, a = i;
    else
      former[i] = b, b = i;
  }
  for (int i = 1; i <= n; i++) {
    if (former[i]) {
      if (s[i] == 'A') {
        if (former[i] > fb && fb && former[i] == i - 1)
          ans += former[i] - 1;
        else
          ans += former[i];
      } else {
        if (former[i] > fa && fa && former[i] == i - 1)
          ans += former[i] - 1;
        else
          ans += former[i];
      }
    }
  }
  printf("%lld\n", ans);
}
