#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1;
int nxt[N], last[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  string s;
  cin >> n >> s;
  int last_a = -1, last_b = -1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'A') last[i] = last_a, last_a = i;
    if (s[i] == 'B') last[i] = last_b, last_b = i;
  }
  last_a = last_b = n;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'A') nxt[i] = last_a, last_a = i;
    if (s[i] == 'B') nxt[i] = last_b, last_b = i;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += nxt[i] - i;
    ans += i - last[i];
    ans--;
    if (i && s[i] != s[i - 1]) ans--;
  }
  ans = 1LL * n * (n + 1) / 2 - ans;
  cout << ans;
  return 0;
}
