#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int a[maxn], k[3];
int main() {
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'A')
      a[i] = 0;
    else
      a[i] = 1;
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    while (k[0] < n && (k[0] <= i || a[k[0]] != 0)) k[0]++;
    while (k[1] < n && (k[1] <= i || a[k[1]] != 1)) k[1]++;
    if (a[i] == a[i + 1]) {
      ans += k[!a[i]] - i - 1;
      if (n - 1 > k[!a[i]]) ans += n - 1 - k[!a[i]];
    } else {
      ans += n - k[a[i]];
    }
  }
  cout << ans << endl;
}
