#include <bits/stdc++.h>
using namespace std;
int last[2];
int main() {
  long long x, ans = 0, n, i;
  string s;
  cin >> n >> s;
  last[0] = last[1] = -1;
  for (i = 0; i < n; i++) {
    x = s[i] - 'A';
    if (last[x] >= 0) {
      ans += last[x] + 1;
      if (last[x ^ 1] != -1) ans -= last[x ^ 1] < last[x];
    }
    last[x] = i;
  }
  cout << ans << '\n';
}
