#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  long long n;
  long long ka = 0, kb = 0, k1 = -1, k2 = -1;
  cin >> n;
  cin >> s;
  vector<long long> a, b;
  if (s[0] == 'A') {
    k1++;
    ka++;
    a.push_back(1);
  } else {
    k2++;
    kb++;
    b.push_back(1);
  }
  for (int i = 1; i < n; i++) {
    if (s[i] - 'A' == 0)
      ka++;
    else
      kb++;
    if (s[i] == 'A')
      if (s[i - 1] == 'B') {
        a.push_back(1);
        k1++;
      } else {
        a[k1]++;
      }
    else if (s[i - 1] == 'A') {
      b.push_back(1);
      k2++;
    } else {
      b[k2]++;
    }
  }
  vector<long long> pref(k1 + 2, 0), pref2(k2 + 2, 0);
  for (int i = 1; i <= k1 + 1; i++) {
    pref[i] = pref[i - 1] + a[i - 1];
  }
  for (int i = 1; i <= k2 + 1; i++) {
    pref2[i] = pref2[i - 1] + b[i - 1];
  }
  long long ans = 0;
  long long z1 = 0, z2 = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'A') {
      ka--;
      ans += ka;
      if (s[i + 1] != 'B' && z2 <= k2) ans += pref2[z2 + 1] - pref2[z2] - 1;
      if (z2 < k2) {
        ans += pref2[k2 + 1] - pref2[z2 + 1];
      }
      if (s[i + 1] == 'B') {
        z1++;
      }
    } else {
      kb--;
      ans += kb;
      if (s[i + 1] != 'A' && z1 <= k1) ans += pref[z1 + 1] - pref[z1] - 1;
      if (z1 < k1) {
        ans += pref[k1 + 1] - pref[z1 + 1];
      }
      if (s[i + 1] == 'A') {
        z2++;
      }
    }
  }
  cout << ans;
}
