#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v, u;
  v.push_back(0);
  u.push_back(0);
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'A') {
      v.push_back(v.back() + 1);
      u.push_back(u.back());
    } else {
      v.push_back(v.back());
      u.push_back(u.back() + 1);
    }
  }
  long long bad = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == 'A') {
      int id = upper_bound(v.begin(), v.end(), v[i]) - v.begin();
      bad += (id - i - 1);
      int id1 = upper_bound(u.begin(), u.end(), u[i]) - u.begin();
      if (id1 == id || s[id1 - 1] != 'B' || id1 == n + 1 || id1 == i + 1)
        continue;
      bad += 1;
    } else {
      int id = upper_bound(u.begin(), u.end(), u[i]) - u.begin();
      bad += (id - i - 1);
      int id1 = upper_bound(v.begin(), v.end(), v[i]) - v.begin();
      if (id1 == id || s[id1 - 1] != 'A' || id1 == n + 1 || id1 == i + 1)
        continue;
      bad += 1;
    }
  }
  long long ans = 1LL * n * (n - 1) / 2 - bad;
  cout << ans;
}
