#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int dp[N];
char s[N];
int nxt[N], pre[N];
long long ans;
int main() {
  int n;
  cin >> n;
  cin >> s + 1;
  int A = 0, B = 0;
  ans = 1ll * (n - 1) * n / 2;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == 'A')
      pre[i] = A, A = i;
    else
      pre[i] = B, B = i;
  }
  A = B = n + 1;
  for (int i = n; i; --i) {
    if (s[i] == 'A')
      nxt[i] = A, A = i;
    else
      nxt[i] = B, B = i;
  }
  for (int i = 1; i <= n; ++i) {
    if (s[i] != s[i + 1] && i < n) ans++;
    ans -= nxt[i] - pre[i] - 2;
  }
  cout << ans << endl;
  return 0;
}
