#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  long long cnt = 0;
  for (int i = 0; i < n;) {
    if (s[i] == 'A') {
      int j = i + 1;
      while (j < n && s[j] == 'B') j++;
      cnt += j - i - 1;
      i = j;
    } else
      i++;
  }
  for (int i = 0; i < n;) {
    if (s[i] == 'B') {
      int j = i + 1;
      while (j < n && s[j] == 'A') j++;
      cnt += j - i - 1;
      i = j;
    } else
      i++;
  }
  reverse(s.begin(), s.end());
  for (int i = 0; i < n;) {
    if (s[i] == 'A') {
      int j = i + 1;
      while (j < n && s[j] == 'B') j++;
      cnt += max(0, j - i - 2);
      i = j;
    } else
      i++;
  }
  for (int i = 0; i < n;) {
    if (s[i] == 'B') {
      int j = i + 1;
      while (j < n && s[j] == 'A') j++;
      cnt += max(0, j - i - 2);
      i = j;
    } else
      i++;
  }
  long long ans = 1LL * n * (n - 1) / 2 - cnt;
  cout << ans << "\n";
}
int main() {
  solve();
  return 0;
}
