#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, i, x, y, n, r, u, res = 0, a, b;
  string s;
  cin >> n;
  cin >> s;
  a = -1;
  b = -1;
  for (i = 0; i < n; i++) {
    if (s[i] == 'A') {
      if (a > -1) {
        res += a + 1;
        if (b < a && b != -1) res--;
      }
    } else {
      if (b > -1) {
        res += b + 1;
        if (a < b && a != -1) res--;
      }
    }
    if (s[i] == 'A')
      a = i;
    else
      b = i;
  }
  cout << res;
  return 0;
}
