#include <bits/stdc++.h>
using namespace std;
const long dx[4] = {1, 0, -1, 0}, dz[4] = {0, 1, 0, -1};
const long double pi = 3.14159265359;
const long long mod = 1e9 + 7;
template <class T>
void re(T& x) {
  cin >> x;
}
template <class T1, class T2>
void re(pair<T1, T2>& x) {
  re(x.first);
  re(x.second);
}
template <class T>
void re(vector<T>& x) {
  for (long i = 0; i < x.size(); i++) {
    re(x[i]);
  }
}
template <class T>
void re(deque<T>& x) {
  for (long i = 0; i < x.size(); i++) {
    re(x[i]);
  }
}
template <class T>
void out(T x) {
  cout << x << " ";
}
template <class T1, class T2>
void out(pair<T1, T2> x) {
  out(x.first);
  out(x.second);
  cout << endl;
}
template <class T>
void out(vector<T> x, long l = 0, long r = 0) {
  if (!r) {
    r = x.size();
  }
  for (long i = l; i < r; i++) {
    out(x[i]);
  }
  cout << endl;
}
template <class T>
void out(deque<T> x, long l = 0, long r = 0) {
  if (!r) {
    r = x.size();
  }
  for (long i = l; i < r; i++) {
    out(x[i]);
  }
  cout << endl;
}
template <class T>
T cross(complex<T> x, complex<T> z) {
  return (conj(x) * z).imag();
}
template <class T>
T dot(complex<T> x, complex<T> z) {
  return (conj(x) * z).real();
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, ans, a = -1, b = -1;
  string s;
  cin >> n >> s;
  ans = (n * (n - 1)) / 2;
  deque<long> x, z;
  for (long long i = (0); i < (n); i++) {
    if (s[i] == 'A') {
      a = i;
      x.push_back(i);
    } else {
      b = i;
      z.push_back(i);
    }
  }
  x.push_back(n);
  z.push_back(n);
  for (long long i = (0); i < (n - 1); i++) {
    if (s[i] == 'A') {
      x.pop_front();
      ans -= (x.front() - 1 - i);
      if (s[i + 1] == 'A') {
        if (i < b) {
          ans--;
        }
      }
      continue;
    }
    if (s[i] == 'B') {
      z.pop_front();
      ans -= (z.front() - 1 - i);
      if (s[i + 1] == 'B') {
        if (i < a) {
          ans--;
        }
      }
    }
  }
  out(ans);
  return 0;
}
