#include <bits/stdc++.h>
using namespace std;
string s;
vector<int> ocA, ocB;
long long answer;
void Solve() {
  if (ocB.empty()) ocB.push_back(INT_MAX);
  for (int A_tr = 0, B_tr = 0; A_tr < ocA.size(); ++A_tr) {
    while (B_tr + 1 < ocB.size() && ocB[B_tr + 1] < ocA[A_tr]) ++B_tr;
    int fAraB = ocB[B_tr] < ocA[A_tr] ? (ocB[B_tr] + 1) : 0;
    answer += ocA[A_tr] - fAraB;
    if (ocB[B_tr] == ocA[A_tr] - 1)
      answer += A_tr > 0 ? (ocA[A_tr - 1] + 1) : 0;
    else
      answer += (ocB[B_tr] < ocA[A_tr]) * ocB[B_tr];
  }
  if (ocB[0] == INT_MAX) ocB.clear();
}
int main() {
  cin >> s >> s;
  for (int i = 0; i < s.length(); ++i)
    if (s[i] == 'A')
      ocA.push_back(i);
    else
      ocB.push_back(i);
  Solve();
  swap(ocA, ocB);
  Solve();
  cout << answer;
  return 0;
}
