#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int n;
char s[500005];
int nxta[500005], nxtb[500005];
int main() {
  scanf("%d", &n);
  scanf("%s", s);
  int pa = 1e9, pb = 1e9;
  for (int i = n - 1; i >= 0; i--) {
    nxta[i] = pa;
    nxtb[i] = pb;
    if (s[i] == 'A')
      pa = i;
    else
      pb = i;
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'A') {
      if (nxta[i] < 1e9) {
        if (s[i] == s[i + 1]) {
          ans += n - i - 1;
          if (nxtb[i] < 1e9) {
            ans -= 1;
          }
        } else {
          ans += n - nxta[i];
        }
      }
    } else {
      if (nxtb[i] < 1e9) {
        if (s[i] == s[i + 1]) {
          ans += n - i - 1;
          if (nxta[i] < 1e9) {
            ans -= 1;
          }
        } else {
          ans += n - nxtb[i];
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
