#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define all(x) x.begin(),x.end()
#define len(x) (int)(x).size()
#define int long long

/*
#pragma GCC omptimize("unroll-loops")
#pragma optimize("SEX_ON_THE_BEACH")
#pragma GCC optimize("no-stack-protector")
#pragma comment(linker, "/STACK:10000000000")
*/

using namespace std;
using namespace __gnu_pbds;

typedef long double ld;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pari;
typedef vector<vi > matrix;

ld PI = 3.14159265358979323846;

typedef tree<int, null_type, less<int >,
    rb_tree_tag, tree_order_statistics_node_update> ordered_set;

void volodya22(){
    #ifdef THERE
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen("fibonacci.in", "r", stdin);
        //freopen("fibonacci.out", "w", stdout);
        #define endl "\n"
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout.precision(6);
    //cout << fixed;
}

const int N = 1e6+7;
const int alphabet = 256;
const ll INF = 2e9+7;
const ll MOD = 1e9+7;
const ld eps = 1e-8;
const int K = 2000+7;

int m1 = 998244353;
int m2 = 1e9+9;
int p1 = 347;
int p2 = 108;

struct Zdarovo{
    int x,c;
};

int n;
string s;
int p[N];
int dp[N];

set<int> A,B;

void f(int i){
    if(s[i] == 'A')
        A.insert(i);
    else B.insert(i);
}

void input(){
    cin >> n >> s;
    set<int> st;

    int ans = 0;
    for(int i = 0;i < n;i++){
        f(i);
    }

     for(int i = n-1;i >= 0;i--){
        if(s[i] == 'A'){
            if(A.upper_bound(i) == A.end())
                dp[i] = 0;
            else{
                if(B.upper_bound(i) == B.end()){
                    dp[i] = n-i-1;
                }else if(B.upper_bound(i) == (--B.end())){
                    dp[i] = n-i-2;
                }else{
                    if(s[i] == s[i+1]){
                        dp[i] = n-i-2;
                    }else{
                        dp[i] = n-*(A.upper_bound(i));
                    }
                }
            }
        }else{
            if(B.upper_bound(i) == B.end())
                dp[i] = 0;
            else{
                if(A.upper_bound(i) == A.end()){
                    dp[i] = n-i-1;
                }else if(A.upper_bound(i) == (--A.end())){
                    dp[i] = n-i-2;
                }else{
                    if(s[i] == s[i+1]){
                        dp[i] = n-i-2;
                    }else{
                        dp[i] = n-*(B.upper_bound(i));
                    }
                }
            }
        }
        //cout << dp[i] << endl;
        ans += dp[i];
    }
    cout << ans;
}

void solve(){

}

signed main(){
    volodya22();
    input();
    solve();
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;

#define FOR(i, a, b) for(ll i = a; i < b; i++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i--)
#define sz(x) (int)(x).size()
#define all(a) a.begin(), a.end()
#define endl '\n';
#define pb push_back
#define ff first
#define ss second

template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T>> &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}

#define INF INT_MAX
#define MOD 1000000007
// #define DEBUG

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
  int n; cin >> n;
  string s; cin >> s;
  vll lastA (n+1, -1);
  vll lastB (n+1, -1);
  vll dp (n, 0);
  FOR(i, 0, n) {
    if (s[i] == 'A') {
      lastA[i+1] = i;
      lastB[i+1] = lastB[i];
    }
    else {
      lastB[i+1] = i;
      lastA[i+1] = lastA[i];
    }
  }
  FOR(i, 1, n) {
    if (s[i] == 'A') {
      if (lastA[i] == -1) {
        continue;
      }
      else if (lastA[i] == i-1) {
        if (lastB[i] == -1) {
          dp[i] = i;
        }
        else if (lastA[lastB[i]] == -1) {
          dp[i] = i - lastB[i] - 1;
          dp[i] += lastB[i];
        }
        else {
          dp[i] = lastA[lastB[i]] + 1;
          dp[i] += i - lastB[i] - 1;
        }
      }
      else {
        dp[i] = lastA[i] + 1;
      }
    }

    if (s[i] == 'B') {
      if (lastB[i] == -1) {
        continue;
      }
      else if (lastB[i] == i-1) {
        if (lastA[i] == -1) {
          dp[i] = i;
        }
        else if (lastB[lastA[i]] == -1) {
          dp[i] = i - lastA[i] - 1;
          dp[i] += lastA[i];
        }
        else {
          dp[i] = lastB[lastA[i]] + 1;
          dp[i] += i - lastA[i] - 1;
        }
      }
      else {
        dp[i] = lastB[i] + 1;
      }
    }
  }

  ll ans = 0;
  FOR(i, 0, n) {
    ans += dp[i];
  }
  pr(ans);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i--)
#define sz(x) (int)(x).size()
#define all(a) a.begin(), a.end()
#define endl '\n';
#define pb push_back
#define ff first
#define ss second

template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T>> &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}

#define INF INT_MAX
#define MOD 1000000007
// #define DEBUG

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
  int n; cin >> n;
  string s; cin >> s;
  vll lastA (n+1, -1);
  vll lastB (n+1, -1);
  vll dp (n, 0);
  FOR(i, 0, n) {
    if (s[i] == 'A') {
      lastA[i+1] = i;
      lastB[i+1] = lastB[i];
    }
    else {
      lastB[i+1] = i;
      lastA[i+1] = lastA[i];
    }
  }
  FOR(i, 1, n) {
    if (s[i] == 'A') {
      if (lastA[i] == -1) {
        continue;
      }
      else if (lastA[i] == i-1) {
        if (lastB[i] == -1) {
          dp[i] = i;
        }
        else if (lastA[lastB[i]] == -1) {
          dp[i] = i - lastB[i] - 1;
          dp[i] += lastB[i];
        }
        else {
          dp[i] = lastA[lastB[i]] + 1;
          dp[i] += i - lastB[i] - 1;
        }
      }
      else {
        dp[i] = lastA[i] + 1;
      }
    }

    if (s[i] == 'B') {
      if (lastB[i] == -1) {
        continue;
      }
      else if (lastB[i] == i-1) {
        if (lastA[i] == -1) {
          dp[i] = i;
        }
        else if (lastB[lastA[i]] == -1) {
          dp[i] = i - lastA[i] - 1;
          dp[i] += lastA[i];
        }
        else {
          dp[i] = lastB[lastA[i]] + 1;
          dp[i] += i - lastA[i] - 1;
        }
      }
      else {
        dp[i] = lastB[i] + 1;
      }
    }
  }

  ll ans = 0;
  FOR(i, 0, n) {
    ans += dp[i];
  }
  pr(ans);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i--)
#define sz(x) (int)(x).size()
#define all(a) a.begin(), a.end()
#define endl '\n';
#define pb push_back
#define ff first
#define ss second

template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T>> &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}

#define INF INT_MAX
#define MOD 1000000007
// #define DEBUG

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
  int n; cin >> n;
  string s; cin >> s;
  vi lastA (n+1, -1);
  vi lastB (n+1, -1);
  vi dp (n, 0);
  FOR(i, 0, n) {
    if (s[i] == 'A') {
      lastA[i+1] = i;
      lastB[i+1] = lastB[i];
    }
    else {
      lastB[i+1] = i;
      lastA[i+1] = lastA[i];
    }
  }
  FOR(i, 1, n) {
    if (s[i] == 'A') {
      if (lastA[i] == -1) {
        continue;
      }
      else if (lastA[i] == i-1) {
        if (lastB[i] == -1) {
          dp[i] = i;
        }
        else if (lastA[lastB[i]] == -1) {
          dp[i] = i - lastB[i] - 1;
          dp[i] += lastB[i];
        }
        else {
          dp[i] = lastA[lastB[i]] + 1;
          dp[i] += i - lastB[i] - 1;
        }
      }
      else {
        dp[i] = lastA[i] + 1;
      }
    }

    if (s[i] == 'B') {
      if (lastB[i] == -1) {
        continue;
      }
      else if (lastB[i] == i-1) {
        if (lastA[i] == -1) {
          dp[i] = i;
        }
        else if (lastB[lastA[i]] == -1) {
          dp[i] = i - lastA[i] - 1;
          dp[i] += lastA[i];
        }
        else {
          dp[i] = lastB[lastA[i]] + 1;
          dp[i] += i - lastA[i] - 1;
        }
      }
      else {
        dp[i] = lastB[i] + 1;
      }
    }
  }

  int ans = 0;
  FOR(i, 0, n) {
    ans += dp[i];
  }
  pr(ans);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vpi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;

#define FOR(i, a, b) for(ll i = a; i < b; i++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i--)
#define sz(x) (int)(x).size()
#define all(a) a.begin(), a.end()
#define endl '\n';
#define pb push_back
#define ff first
#define ss second

template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T>> &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}

#define INF INT_MAX
#define MOD 1000000007
// #define DEBUG

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
  ll n; cin >> n;
  string s; cin >> s;
  vll lastA (n+1, -1);
  vll lastB (n+1, -1);
  vll dp (n, 0);
  FOR(i, 0, n) {
    if (s[i] == 'A') {
      lastA[i+1] = i;
      lastB[i+1] = lastB[i];
    }
    else {
      lastB[i+1] = i;
      lastA[i+1] = lastA[i];
    }
  }
  FOR(i, 1, n) {
    if (s[i] == 'A') {
      if (lastA[i] == -1) {
        continue;
      }
      else if (lastA[i] == i-1) {
        if (lastB[i] == -1) {
          dp[i] = i;
        }
        else if (lastA[lastB[i]] == -1) {
          dp[i] = i - lastB[i] - 1;
          dp[i] += lastB[i];
        }
        else {
          dp[i] = lastB[i];
          dp[i] += i - lastB[i] - 1;
        }
      }
      else {
        dp[i] = lastA[i] + 1;
      }
    }

    if (s[i] == 'B') {
      if (lastB[i] == -1) {
        continue;
      }
      else if (lastB[i] == i-1) {
        if (lastA[i] == -1) {
          dp[i] = i;
        }
        else if (lastB[lastA[i]] == -1) {
          dp[i] = i - lastA[i] - 1;
          dp[i] += lastA[i];
        }
        else {
          dp[i] = lastA[i];
          dp[i] += i - lastA[i] - 1;
        }
      }
      else {
        dp[i] = lastB[i] + 1;
      }
    }
  }

  ll ans = 0;
  FOR(i, 0, n) {
    ans += dp[i];
  }
  pr(ans);
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define mod 1000 * 1000 * 1000 + 7
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n, ans = 0, count = 1, hm=1;
    string s;
    cin >> n >> s;
    ans=n * (n - 1)/2;
    vector<ll>A;
    rep(i,1,n)
    {
        if(s[i]==s[i-1])
            hm++;
        else
        {
            A.pb(hm);
            hm=1;
        }
    }
    A.pb(hm);
    rep(i,0,A.size()-1)
    {
        ans-=A[i];
    }
    rep2(i,A.size(), 1)
    {
        ans-=A[i]-1;
    }
    // rep(i, 1, n)
    // {
    //     if (s[i] != s[i - 1])
    //         ans -= count, count = 0;
    //     count++;
    // }
    // count = 1;
    // rep2(i, n - 1, 0)
    // {
    //     if (s[i] != s[i + 1])
    //         ans -= count-1, count = 0;
    //     count++;
    // }
    cout << ans  << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define mod 1000 * 1000 * 1000 + 7
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, ans = 0, count = 1;
    string s;
    cin >> n >> s;
    ans=n * (n - 1)/2;
    rep(i, 1, n)
    {
        if (s[i] != s[i - 1])
            ans -= count, count = 0;
        count++;
    }
    count = 1;
    rep2(i, n - 1, 0)
    {
        if (s[i] != s[i + 1])
            ans -= count-1, count = 0;
        count++;
    }
    cout << ans  << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define mod 1000 * 1000 * 1000 + 7
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n, ans = 0, count = 1, hm=1;
    string s;
    cin >> n >> s;
    ans=n * (n - 1)/2;
    vector<ll>A;
    rep(i,1,n)
    {
        if(s[i]==s[i-1])
            hm++;
        else
        {
            A.pb(hm);
            hm=1;
        }
    }
    A.pb(hm);
    rep(i,0,A.size()-1)
    {
        ans-=A[i]*A[i+1];
    }
    // rep(i, 1, n)
    // {
    //     if (s[i] != s[i - 1])
    //         ans -= count, count = 0;
    //     count++;
    // }
    // count = 1;
    // rep2(i, n - 1, 0)
    // {
    //     if (s[i] != s[i + 1])
    //         ans -= count-1, count = 0;
    //     count++;
    // }
    cout << ans  << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define mod 1000 * 1000 * 1000 + 7
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n, ans = 0, count = 1;
    string s;
    cin >> n >> s;
    ans=n * (n - 1)/2;
    rep(i, 1, n)
    {
        if (s[i] != s[i - 1])
            ans -= count, count = 0;
        count++;
    }
    count = 1;
    rep2(i, n - 1, 0)
    {
        if (s[i] != s[i + 1])
            ans -= count-1, count = 0;
        count++;
    }
    cout << ans  << '\n';
}


#include <iostream>
#include <string>
using namespace std;
using int64 = long long;
int main() {
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  int n;cin>>n;string s;cin>>s;
  auto r=0ll,n_a=-1ll,n_b=-1ll;
  for (auto i=1;i<n;i++){
    auto p=s[i-1],c=s[i];
    if (p=='A') n_a=i-1; else n_b=i-1;
    if (c=='A' and p=='A') {r+=n_a+(n_b<0);}
    if (c=='A' and p=='B') {r+=n_a+1;}
    if (c=='B' and p=='A') {r+=n_b+1;}
    if (c=='B' and p=='B') {r+=n_b+(n_a<0);}
  }
  cout<<r;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
using int64 = long long;
int main() {
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  int n;cin>>n;string s;cin>>s;
  char p=s[0],c;auto n_a=p=='A',n_b=p=='B';auto r=0ll;
  for(auto i=1;i<n;i++,p=c,n_a|=c=='A',n_b|=c=='B'){
    c=s[i];
    if (c=='A' and p=='A') {r+=i;if(n_b)r--;}
    if (c=='A' and p=='B') {if(n_a)r+=i-1;}
    if (c=='B' and p=='A') {if(n_b)r+=i-1;}
    if (c=='B' and p=='B') {r+=i;if(n_a)r--;}
  }
  cout<<r;
}


#include <cstdio>
int main() {
  int n;scanf("%d\n",&n);
  auto r=0ll,n_a=-1ll,n_b=-1ll;
  char p=getchar_unlocked(),c;
  for (auto i=1;i<n;i++,p=c){
    c=getchar_unlocked();*((p=='A')?&n_a:&n_b)=i-1;
    if (c=='A' and p=='A') {r+=n_a+(n_b<0);}
    if (c=='A' and p=='B') {r+=n_a+1;}
    if (c=='B' and p=='A') {r+=n_b+1;}
    if (c=='B' and p=='B') {r+=n_b+(n_a<0);}
  }
  printf("%lld",r);
}


#include <iostream>
#include <string>
using namespace std;
using int64 = long long;
int main() {
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  int n;cin>>n;string s;cin>>s;
  auto p=s[0];
  auto n_a=p=='A',n_b=p=='B';
  auto r=0ll;
  for (auto i=1;i<n;i++){
    auto c=s[i];
    if (c=='A' and p=='A') {r+=i;if(n_b)r--;}
    if (c=='A' and p=='B') {if(n_a)r+=i-1;}
    if (c=='B' and p=='A') {if(n_b)r+=i-1;}
    if (c=='B' and p=='B') {r+=i;if(n_a)r--;}
    n_a|=c=='A';n_b|=c=='B';p=c;
  }
  cout<<r;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
using int64 = long long;
int main() {
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  int n;cin>>n;string s;cin>>s;
  auto p=s[0];auto n_a=p=='A',n_b=p=='B';auto r=0ll;
  for (auto i=1;i<s.size();i++){
    auto c=s[i];
    if (c=='A' and p=='A') {r+=i;if(n_b)r--;}
    if (c=='A' and p=='B') {if(n_a)r+=i-1;}
    if (c=='B' and p=='A') {if(n_b)r+=i-1;}
    if (c=='B' and p=='B') {r+=i;if(n_a)r--;}
    n_a|=c=='A';n_b|=c=='B';
  }
  cout<<r;
}


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;
#define int long long
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

//vi x = { 0, 0, -1, 1 };
//vi y = { -1, 1, 0, 0 };

const int N = 5e5 + 50, oo = 1e18 + 100, mod = 1e9 + 7;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
int cnt = 0;
vi g[N];

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("sum.in", "r", stdin);
	freopen("sum.out", "w", stdout);*/

	cin >> n;
	string s;
	cin >> s;

	vi vc;
	for (int i = 0; i < n; i++)
	{
		int j = i;
		while (j + 1 < n && s[j] == s[j + 1])
		{
			j++;
		}

		vc.pb(j - i + 1);
		i = j;
	}

	for (int i = 1; i < SZ(vc); i++)
	{
		cnt += vc[i] + vc[i-1] - 1;
	}

	cout << n*(n-1)/2 - cnt;
}

//fuck


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;
#define int long long
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

//vi x = { 0, 0, -1, 1 };
//vi y = { -1, 1, 0, 0 };

const int N = 5e5 + 50, oo = 1e18 + 100, mod = 1e9 + 7;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
int cnt = 0;
vi g[N];

int t[N];

void add(int ind, int val)
{
	for (int i = ind; i < N; i += i&-i)
	{
		t[i] += val;
	}
}

int get(int ind)
{
	int ret = 0;
	for (int i = ind; i ; i -= i&-i)
	{
		ret += t[i];
	}
	return ret;
}

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("sum.in", "r", stdin);
	freopen("sum.out", "w", stdout);*/

	cin >> n;

	string s;
	cin >> s;

	vi end(n, 0);
	int last0 = -1, last1 = -1;
	vi nxt(n, -1);

	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'A')
		{
			if (last0 != -1)
			{
				nxt[last0] = i;
			}
			last0 = i;
		}
		else
		{
			if (last1 != -1)
			{
				nxt[last1] = i;
			}
			last1 = i;
		}
	}

	for (int i = n - 1; i >= 0; i--)
	{
		if (nxt[i] != -1)
		{
			add(nxt[i] + 1, 1);

			cnt += get(N - 1) - get(nxt[i]);
		}
	}

	cout << cnt;
}

//fuck


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;
#define int long long
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

//vi x = { 0, 0, -1, 1 };
//vi y = { -1, 1, 0, 0 };

const int N = 5e5 + 50, oo = 1e18 + 100, mod = 1e9 + 7;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
int cnt = 0;
vi g[N];

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("sum.in", "r", stdin);
	freopen("sum.out", "w", stdout);*/

	cin >> n;

	string s;
	cin >> s;

	vi end(n, 0);
	int last0 = -1, last1 = -1;
	vi nxt(n, -1);

	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'A')
		{
			if (last0 != -1)
			{
				end[i]++;
				nxt[last0] = i;
			}
			last0 = i;
		}
		else
		{
			if (last1 != -1)
			{
				end[i]++;
				nxt[last1] = i;
			}
			last1 = i;
		}
	}

	for (int i = n - 2; i >= 0; i--)
	{
		end[i] += end[i + 1];
	}

	for (int i = 0; i < n; i++)
	{
		if (nxt[i] != -1)
		{
			cnt += end[nxt[i]];
		}
	}

	cout << cnt;
}

//fuck


//#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>
#include "random"

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++)
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;
#define int long long
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;
typedef vector<vi> vvi;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

//vi x = { 0, 0, -1, 1 };
//vi y = { -1, 1, 0, 0 };

const int N = 5e5 + 50, oo = 1e18 + 100, mod = 1e9 + 7;
const double eps = 1e-9, PI = 2 * acos(0.0);
int n, m, k;
int cnt = 0;
vi g[N];

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("sum.in", "r", stdin);
	freopen("sum.out", "w", stdout);*/

	cin >> n;

	string s;
	cin >> s;

	vi end(n, 0);

	for (int i = 0; i < n - 1; i++)
	{
		if (s[i] == s[i + 1])
		{
			end[i + 1]++;
		}
		else
		{
			if (i - 1 >= 0 && s[i - 1] == s[i + 1])
			{
				end[i + 1]++;
			}
		}
	}

	for (int i = n - 2; i >= 0; i--)
	{
		end[i] += end[i + 1];
	}

	for (int i = 0; i < n - 1; i++)
	{
		if (s[i] == s[i + 1])
		{
			cnt += end[i + 1];
		}
		else
		{
			if (i + 2 < n && s[i] == s[i+2])
			{
				cnt += end[i + 2];
			}
		}
	}

	cout << cnt;
}

//fuck


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ordered_set tree < ll ,  null_type ,  less<ll> ,  rb_tree_tag ,  tree_order_statistics_node_update >
//added two more functions to set
//(1)*(set.find_by_order(k))[kth element in the sorted set]
//(2)set.order_of_key(k)[count of elements strictly less than k]
typedef vector< int > vi;
typedef vector< ll > lvi;
typedef vector< vi > vvi;
typedef vector< lvi > lvvi;
typedef pair< int,int > ii;
typedef pair< ll,ll > lii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)//remember i is an iterator
#define present(c,x) ((c).find(x) != (c).end())//for sets,maps,multimaps
#define cpresent(c,x) (find(all(c),x) != (c).end())//for vectors
#define tc(t) int (t); cin>>(t);while((t)--)
#define ff first
#define ss second
#define mp make_pair
#define ld long double
#define ull unsigned long long int
#define mod 1000000007
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll n;
	cin>>n;
	string s;
	cin>>s;
	ll rfa=-1,rfb=-1;
	ll ans=(n*(n+1))/2;
	ans--;
	char prev=s[0];
	if(prev=='A') rfa=0;
	else rfb=0;
	for(ll i=1;i<n;i++)
	{
		if(s[i]=='A')
		{
			if(prev=='A')
			{
				ans--;
				if(rfb!=-1) ans--;
			}
			else
			{
				ans-=(i-rfa);
			}
			rfa=i;
		}
		else
		{
			if(prev=='B')
			{
				ans--;
				if(rfa!=-1) ans--;
			}
			else
			{
				ans-=(i-rfb);
			}
			rfb=i;
		}
		prev=s[i];
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ordered_set tree < ll ,  null_type ,  less<ll> ,  rb_tree_tag ,  tree_order_statistics_node_update >
//added two more functions to set
//(1)*(set.find_by_order(k))[kth element in the sorted set]
//(2)set.order_of_key(k)[count of elements strictly less than k]
typedef vector< int > vi;
typedef vector< ll > lvi;
typedef vector< vi > vvi;
typedef vector< lvi > lvvi;
typedef pair< int,int > ii;
typedef pair< ll,ll > lii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)//remember i is an iterator
#define present(c,x) ((c).find(x) != (c).end())//for sets,maps,multimaps
#define cpresent(c,x) (find(all(c),x) != (c).end())//for vectors
#define tc(t) int (t); cin>>(t);while((t)--)
#define ff first
#define ss second
#define mp make_pair
#define ld long double
#define ull unsigned long long int
#define mod 1000000007
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll n;
	cin>>n;
	string s;
	cin>>s;
	ll rfa=-1,rfb=-1;
	ll ans=(n*(n+1))/2;
	ans--;
	char prev=s[0];
	for(ll i=1;i<n;i++)
	{
		if(s[i]=='A')
		{
			if(prev=='A')
			{
				ans--;
				if(rfb!=-1) ans--;
			}
			else
			{
				ans-=(i-rfa);
			}
			rfa=i;
		}
		else
		{
			if(prev=='B')
			{
				ans--;
				if(rfa!=-1) ans--;
			}
			else
			{
				ans-=(i-rfb);
			}
			rfb=i;
		}
		prev=s[i];
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ordered_set tree < ll ,  null_type ,  less<ll> ,  rb_tree_tag ,  tree_order_statistics_node_update >
//added two more functions to set
//(1)*(set.find_by_order(k))[kth element in the sorted set]
//(2)set.order_of_key(k)[count of elements strictly less than k]
typedef vector< int > vi;
typedef vector< ll > lvi;
typedef vector< vi > vvi;
typedef vector< lvi > lvvi;
typedef pair< int,int > ii;
typedef pair< ll,ll > lii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)//remember i is an iterator
#define present(c,x) ((c).find(x) != (c).end())//for sets,maps,multimaps
#define cpresent(c,x) (find(all(c),x) != (c).end())//for vectors
#define tc(t) int (t); cin>>(t);while((t)--)
#define ff first
#define ss second
#define mp make_pair
#define ld long double
#define ull unsigned long long int
#define mod 1000000007
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int n;
	cin>>n;
	string s;
	cin>>s;
	int rfa=-1,rfb=-1;
	ll ans=(n*(n+1))/2;
	ans--;
	char prev=s[0];
	for(int i=1;i<n;i++)
	{
		if(s[i]=='A')
		{
			if(prev=='A')
			{
				ans--;
				if(rfb!=-1) ans--;
			}
			else
			{
				ans-=(i-rfa);
			}
			rfa=i;
		}
		else
		{
			if(prev=='B')
			{
				ans--;
				if(rfa!=-1) ans--;
			}
			else
			{
				ans-=(i-rfb);
			}
			rfb=i;
		}
		prev=s[i];
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ordered_set tree < ll ,  null_type ,  less<ll> ,  rb_tree_tag ,  tree_order_statistics_node_update >
//added two more functions to set
//(1)*(set.find_by_order(k))[kth element in the sorted set]
//(2)set.order_of_key(k)[count of elements strictly less than k]
typedef vector< int > vi;
typedef vector< ll > lvi;
typedef vector< vi > vvi;
typedef vector< lvi > lvvi;
typedef pair< int,int > ii;
typedef pair< ll,ll > lii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)//remember i is an iterator
#define present(c,x) ((c).find(x) != (c).end())//for sets,maps,multimaps
#define cpresent(c,x) (find(all(c),x) != (c).end())//for vectors
#define tc(t) int (t); cin>>(t);while((t)--)
#define ff first
#define ss second
#define mp make_pair
#define ld long double
#define ull unsigned long long int
#define mod 1000000007
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll n;
	cin>>n;
	string s;
	cin>>s;
	int rfa=-1,rfb=-1;
	ll ans=(n*(n+1))/2;
	ans--;
	char prev=s[0];
	for(int i=1;i<n;i++)
	{
		if(s[i]=='A')
		{
			if(prev=='A')
			{
				ans--;
				if(rfb!=-1) ans--;
			}
			else
			{
				ans-=(i-rfa);
			}
			rfa=i;
		}
		else
		{
			if(prev=='B')
			{
				ans--;
				if(rfa!=-1) ans--;
			}
			else
			{
				ans-=(i-rfb);
			}
			rfb=i;
		}
		prev=s[i];
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 300 * 1000 + 20;

ll n, dp[MAXN], dp2[MAXN], pa, pb, res;
string s;

int main() {
    ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n >> s;
    for (int i = 1; i < n; i++) {
        if (s[i] != s[i - 1])
            dp[i] += 1;
        else
            dp[i] += dp[i - 1];
    }
    for (int i = n - 2; i >= 0; i--) {
        if (s[i] != s[i + 1])
            dp2[i]++;
        else
            dp2[i] += dp2[i + 1];
    }
    for (int i = 0; i < n; i++) {
        res += dp[i];
        if (dp2[i] && s[i] == s[i + 1])
            res += dp2[i];
    }
    cout << n * (n - 1) / 2 - res << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 300 * 1000 + 20;

ll n, pa, pb, dp[MAXN], ans, cnta, cntb;
char s[MAXN];

int main() {
    ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> s[i];
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'A') {
            if (pa)
                dp[i] = max({dp[i - 1] * (cnta > 1), dp[pa], dp[pa - 1]}) + 1, pa = i, cnta++;
            else
                pa = i, cnta++;
        }
        else {
            if (pb)
                dp[i] = max({dp[i - 1] * (cntb > 1), dp[pb], dp[pb - 1]}) + 1, pb = i, cntb++;
            else
                pb = i, cntb++;
        }
    }
    for (int i = 1; i <= n; i++)
        ans += dp[i];
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 300 * 1000 + 20;

ll n, pa, pb, dp[MAXN], ans, cnta, cntb;
char s[MAXN];

int main() {
    ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> s[i];
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'A') {
            if (pa)
                dp[i] = max({dp[i - 1] * (dp[pa + 1] > 0), dp[pa], dp[pa - 1]}) + 1, pa = i;
            else
                pa = i;
        }
        else {
            if (pb)
                dp[i] = max({dp[i - 1] * (dp[pb + 1] > 0), dp[pb], dp[pb - 1]}) + 1, pb = i;
            else
                pb = i;
        }
    }
    for (int i = 1; i <= n; i++)
        ans += dp[i];
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 300 * 1000 + 20;

ll n, dp[MAXN], dp2[MAXN], pa, pb, res;
string s;

int main() {
    ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n >> s;
    for (int i = 1; i < n; i++) {
        if (s[i] != s[i - 1])
            dp[i] = 1;
        else
            dp[i] = dp[i - 1];
    }
    for (int i = n - 2; i >= 0; i--) {
        if (s[i] != s[i + 1])
            dp2[i]++;
        else
            dp2[i] = dp2[i + 1];
    }
    for (int i = 0; i < n; i++) {
        res += dp[i];
        if (dp2[i] && !dp[i + 1])
            res += dp2[i];
    }
    cout << n * (n - 1) / 2 - res << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 300 * 1000 + 20;

ll n, dp[MAXN], dp2[MAXN], pa, pb, res;
string s;

int main() {
    ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n >> s;
    for (int i = 1; i < n; i++) {
        if (s[i] != s[i - 1])
            dp[i] += 1;
        else
            dp[i] += dp[i - 1];
    }
    for (int i = n - 2; i >= 0; i--) {
        if (s[i] != s[i + 1])
            dp2[i]++;
        else
            dp2[i] += dp2[i + 1];
    }
    for (int i = 0; i < n; i++) {
        res += dp[i];
        if (dp2[i] && !dp[i + 1])
            res += dp2[i];
    }
    cout << n * (n - 1) / 2 - res << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 300 * 1000 + 20;

ll n, pa, pb, dp[MAXN], ans;
char s[MAXN];

int main() {
    ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> s[i];
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'A') {
            if (pa)
                dp[i] = max({dp[i - 1], dp[pa], dp[pa - 1]}) + 1, pa = i;
            else
                pa = i;
        }
        else {
            if (pb) {
                dp[i] = max({dp[i - 1], dp[pa], dp[pa - 1]}) + 1, pb = i;
            }
            else
                pb = i;
        }
    }
    for (int i = 1; i <= n; i++)
        ans += dp[i];
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
string s;
ll n;
int main(){
    cin >> n;
    cin >> s;
    ll ans=n*(n-1)/2;
    for(int x=0;x<2;x++){
        ll cnt=1;
        for(ll i=0;i<n-1;i++){
            if(s[i]==s[i+1]){
                cnt++;
            }
            else{
                ans-=cnt-x;
                cnt=1;
            }
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
string s;
ll n;
int main(){
    cin >> n;
    cin >> s;
    ll ans=n*(n-1)/2;
    for(int x=0;x<2;x++){
        ll cnt=1;
        for(ll i=0;i<n-1;i++){
            if(s[i]==s[i+1]){
                cnt++;
            }
            else{
                ans-=cnt-x;
                cnt=1;
            }
        }
        for(ll i=0;i<n/2;i++){
            swap(s[i],s[n-i-1]);
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
string s;
ll n;
int main(){
    cin >> n;
    cin >> s;
    ll ans=0;
    if (n%2==0){
        ans=n/2;
        ans*=(n-1);
    }
    else{
        ans=(n-1)/2;
        ans*=n;
    }
    for(int x=0;x<2;x++){
        ll cnt=1;
        for(ll i=1;i<n;i++){
            if(s[i]==s[i-1]){
                cnt++;
            }
            else{
                ans-=cnt-x;
                cnt=1;
            }
        }
        for(ll i=0;i<n/2;i++){
            swap(s[i],s[n-i-1]);
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
string s;
ll n;
int main(){
    cin >> n;
    cin >> s;
    ll ans=0;
    if (n%2==0){
        ans=n/2;
        ans*=(n-1);
    }
    else{
        ans=(n-1)/2;
        ans*=n;
    }
    for(int x=0;x<2;x++){
        ll cnt=1;
        for(ll i=0;i<n-1;i++){
            if(s[i]==s[i+1]){
                cnt++;
            }
            else{
                ans-=cnt-x;
                cnt=1;
            }
        }
        for(ll i=0;i<n/2;i++){
            swap(s[i],s[n-i-1]);
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
string s;
int main(){
    cin >> n;
    cin >> s;
    ll ans=n*(n-1)/2;
    for(int x=0;x<2;x++){
        ll cnt=1;
        for(ll i=1;i<n;i++){
            if(s[i]==s[i-1]){
                cnt++;
            }
            else{
                ans-=cnt-x;
                cnt=1;
            }
        }
        for(ll i=0;i<n/2;i++){
            swap(s[i],s[n-i-1]);
        }
    }
    cout << ans << endl;
    return 0;
}


//  main.cpp
//  compeitive_coding
//
//  Created by Ajay Chauhan
//  Copyright © 2020 Ajay Chauhan. All rights reserved.
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldo;
#define endl            "\n"
#define pi              3.1415926535897932384626433832795
#define mod             (ll)1000000007
#define pp              pair<ll,ll>
#define vii             vector<int>
#define vll             vector<ll>
#define vpp             vector<pp>
#define vss             vector<string>
#define pb              push_back
#define pob             pop_back
#define mp              make_pair
#define pf              push_front
#define pof             pop_front
#define eb              emplace_back
#define ff              first
#define ss              second
//#define A(a,b)          vector<vector<ll>>A(a,vector<ll>(b,0))
#define bs(v,x)         binary_search(v.begin(),v.end(),x)
#define ub(v,x)       upper_bound(v.begin(),v.end(),x)
#define lb(v,x)       lower_bound(v.begin(),v.end(),x)
#define print(A,n)      for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n)       for(ll i=0;i<n;++i)cin>>A[i];
#define set(x)          memset(x,0,sizeof(x));
#define Sort(x)         sort(x.begin(),x.end())
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);
#define setd(s,s1,S1)   set_difference(s.begin(),s.end(),s1.begin(),s1.end(),inserter(S1,S1.end())); //set and multi set
//nth_element(v.begin(), v.begin()+x, v.end(),greater<int>());
//x+1 th greater element in the array

//ll power(ll x, ll y, ll p)
//{
//    ll res = 1;
//
//    x = x % p;
//
//    if (x == 0) return 0;
//
//    while (y > 0)
//    {
//        if (y & 1)
//            res = (res*x) % p;
//        y = y>>1;
//        x = (x*x) % p;
//    }
//    return res;
//}

int main()
{
    fast;
    ll t=1;
//    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        ll n;
        cin>>n;
        string s;
        cin>>s;
        ll ans=n*(n-1)/2;
        ll f1=1,f2=0;
        for(ll i=1;i<n;i++){
            if(s[i]==s[i-1]){
                f1++;
            }
            else
            {
                ans-=f1;
                f1=1;
            }
        }
        for(ll j=n-2;j>=0;j--){
            if(s[j]==s[j+1]){
                f2++;
//                break;
            }
            else{
                ans-=f2;
                f2=0;

            }
        }
        cout<<ans<<endl;
    }
}


//  main.cpp
//  compeitive_coding
//
//  Created by Ajay Chauhan
//  Copyright © 2020 Ajay Chauhan. All rights reserved.
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldo;
#define endl            "\n"
#define pi              3.1415926535897932384626433832795
#define mod             (ll)1000000007
#define pp              pair<ll,ll>
#define vii             vector<int>
#define vll             vector<ll>
#define vpp             vector<pp>
#define vss             vector<string>
#define pb              push_back
#define pob             pop_back
#define mp              make_pair
#define pf              push_front
#define pof             pop_front
#define eb              emplace_back
#define ff              first
#define ss              second
//#define A(a,b)          vector<vector<ll>>A(a,vector<ll>(b,0))
#define bs(v,x)         binary_search(v.begin(),v.end(),x)
#define ub(v,x)       upper_bound(v.begin(),v.end(),x)
#define lb(v,x)       lower_bound(v.begin(),v.end(),x)
#define print(A,n)      for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n)       for(ll i=0;i<n;++i)cin>>A[i];
#define set(x)          memset(x,0,sizeof(x));
#define Sort(x)         sort(x.begin(),x.end())
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);
#define setd(s,s1,S1)   set_difference(s.begin(),s.end(),s1.begin(),s1.end(),inserter(S1,S1.end())); //set and multi set
//nth_element(v.begin(), v.begin()+x, v.end(),greater<int>());
//x+1 th greater element in the array

//ll power(ll x, ll y, ll p)
//{
//    ll res = 1;
//
//    x = x % p;
//
//    if (x == 0) return 0;
//
//    while (y > 0)
//    {
//        if (y & 1)
//            res = (res*x) % p;
//        y = y>>1;
//        x = (x*x) % p;
//    }
//    return res;
//}

int main()
{
    fast;
    ll t=1;
//    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        ll n;
        cin>>n;
        string s;
        cin>>s;
        ll ans=n*(n-1)/2;
        ll f1=1,f2=1;
        for(ll i=1;i<n;i++){
            if(s[i]!=s[i-1]){
                f1++;
            }
            else
            {
                ans-=f1;
                f1=1;
            }
        }
        for(ll j=n-2;j>=0;j--){
            if(s[j]!=s[n-1]){
                f2++;
//                break;
            }
            else{
                ans-=f2;
                f2=1;

            }
        }
        cout<<ans<<endl;
    }
}


//  main.cpp
//  compeitive_coding
//
//  Created by Ajay Chauhan
//  Copyright © 2020 Ajay Chauhan. All rights reserved.
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldo;
#define endl            "\n"
#define pi              3.1415926535897932384626433832795
#define mod             (ll)1000000007
#define pp              pair<ll,ll>
#define vii             vector<int>
#define vll             vector<ll>
#define vpp             vector<pp>
#define vss             vector<string>
#define pb              push_back
#define pob             pop_back
#define mp              make_pair
#define pf              push_front
#define pof             pop_front
#define eb              emplace_back
#define ff              first
#define ss              second
//#define A(a,b)          vector<vector<ll>>A(a,vector<ll>(b,0))
#define bs(v,x)         binary_search(v.begin(),v.end(),x)
#define ub(v,x)       upper_bound(v.begin(),v.end(),x)
#define lb(v,x)       lower_bound(v.begin(),v.end(),x)
#define print(A,n)      for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n)       for(ll i=0;i<n;++i)cin>>A[i];
#define set(x)          memset(x,0,sizeof(x));
#define Sort(x)         sort(x.begin(),x.end())
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);
#define setd(s,s1,S1)   set_difference(s.begin(),s.end(),s1.begin(),s1.end(),inserter(S1,S1.end())); //set and multi set
//nth_element(v.begin(), v.begin()+x, v.end(),greater<int>());
//x+1 th greater element in the array

//ll power(ll x, ll y, ll p)
//{
//    ll res = 1;
//
//    x = x % p;
//
//    if (x == 0) return 0;
//
//    while (y > 0)
//    {
//        if (y & 1)
//            res = (res*x) % p;
//        y = y>>1;
//        x = (x*x) % p;
//    }
//    return res;
//}

int main()
{
    fast;
    ll t=1;
//    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        ll n;
        cin>>n;
        string s;
        cin>>s;
        ll ans=n*(n-1)/2;
        ll f1=1,f2=1;
        for(ll i=1;i<n;i++){
            if(s[i]!=s[i-1]){
                f1++;
            }
            else
            {
                ans-=f1;
                f1=1;
            }
        }
        for(ll j=n-2;j>=0;j--){
            if(s[j]!=s[j+1]){
                f2++;
//                break;
            }
            else{
                ans-=f2;
                f2=1;

            }
        }
        cout<<ans<<endl;
    }
}


//  main.cpp
//  compeitive_coding
//
//  Created by Ajay Chauhan
//  Copyright © 2020 Ajay Chauhan. All rights reserved.
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldo;
#define endl            "\n"
#define pi              3.1415926535897932384626433832795
#define mod             (ll)1000000007
#define pp              pair<ll,ll>
#define vii             vector<int>
#define vll             vector<ll>
#define vpp             vector<pp>
#define vss             vector<string>
#define pb              push_back
#define pob             pop_back
#define mp              make_pair
#define pf              push_front
#define pof             pop_front
#define eb              emplace_back
#define ff              first
#define ss              second
//#define A(a,b)          vector<vector<ll>>A(a,vector<ll>(b,0))
#define bs(v,x)         binary_search(v.begin(),v.end(),x)
#define ub(v,x)       upper_bound(v.begin(),v.end(),x)
#define lb(v,x)       lower_bound(v.begin(),v.end(),x)
#define print(A,n)      for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n)       for(ll i=0;i<n;++i)cin>>A[i];
#define set(x)          memset(x,0,sizeof(x));
#define Sort(x)         sort(x.begin(),x.end())
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);
#define setd(s,s1,S1)   set_difference(s.begin(),s.end(),s1.begin(),s1.end(),inserter(S1,S1.end())); //set and multi set
//nth_element(v.begin(), v.begin()+x, v.end(),greater<int>());
//x+1 th greater element in the array

//ll power(ll x, ll y, ll p)
//{
//    ll res = 1;
//
//    x = x % p;
//
//    if (x == 0) return 0;
//
//    while (y > 0)
//    {
//        if (y & 1)
//            res = (res*x) % p;
//        y = y>>1;
//        x = (x*x) % p;
//    }
//    return res;
//}

int main()
{
    fast;
    ll t=1;
//    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        ll n;
        cin>>n;
        string s;
        cin>>s;
        ll ans=n*(n-1)/2;
        ll f1=0,f2=0;
        for(ll i=1;i<n;i++){
            if(s[i]==s[0]){
                f1=1;
                break;
            }
        }
        for(ll j=n-2;j>=0;j--){
            if(s[i]==s[n-1]){
                f2=1;
                break;
            }
        }
//        ans-=n;
        if(!f1)
            ans-=n-1;
        if(!f2)
            ans-=n-1;
        cout<<ans<<endl;
    }
}


//  main.cpp
//  compeitive_coding
//
//  Created by Ajay Chauhan
//  Copyright © 2020 Ajay Chauhan. All rights reserved.
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldo;
#define endl            "\n"
#define pi              3.1415926535897932384626433832795
#define mod             (ll)1000000007
#define pp              pair<ll,ll>
#define vii             vector<int>
#define vll             vector<ll>
#define vpp             vector<pp>
#define vss             vector<string>
#define pb              push_back
#define pob             pop_back
#define mp              make_pair
#define pf              push_front
#define pof             pop_front
#define eb              emplace_back
#define ff              first
#define ss              second
//#define A(a,b)          vector<vector<ll>>A(a,vector<ll>(b,0))
#define bs(v,x)         binary_search(v.begin(),v.end(),x)
#define ub(v,x)       upper_bound(v.begin(),v.end(),x)
#define lb(v,x)       lower_bound(v.begin(),v.end(),x)
#define print(A,n)      for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n)       for(ll i=0;i<n;++i)cin>>A[i];
#define set(x)          memset(x,0,sizeof(x));
#define Sort(x)         sort(x.begin(),x.end())
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);
#define setd(s,s1,S1)   set_difference(s.begin(),s.end(),s1.begin(),s1.end(),inserter(S1,S1.end())); //set and multi set
//nth_element(v.begin(), v.begin()+x, v.end(),greater<int>());
//x+1 th greater element in the array

//ll power(ll x, ll y, ll p)
//{
//    ll res = 1;
//
//    x = x % p;
//
//    if (x == 0) return 0;
//
//    while (y > 0)
//    {
//        if (y & 1)
//            res = (res*x) % p;
//        y = y>>1;
//        x = (x*x) % p;
//    }
//    return res;
//}

int main()
{
    fast;
    ll t=1;
//    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        ll n;
        cin>>n;
        string s;
        cin>>s;
        ll ans=n*(n+1)/2;
        ll f1=0,f2=0;
        for(ll i=1;i<n;i++){
            if(s[i]==s[0]){
                f1=1;
                break;
            }
        }
        for(ll j=n-2;j>=0;j--){
            if(s[j]==s[n-1]){
                f2=1;
                break;
            }
        }
        ans-=n;
        if(!f1)
            ans-=n-1;
        if(!f2)
            ans-=n-1;
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    long long int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int> sw;
    if(n<2) {cout<<0; return 0;}
    long long int sum = n*(n-1)/2;
    for(int i=1; i<n; i++){
        if(s[i]!=s[i-1]) sw.push_back(i+1);
    }

if(!sw.empty())
  {   sum-=sw[0]-1;

    for(int i=0; i<sw.size()-1; i++){
        sum-=2*(sw[i+1]-sw[i])-1;
    }

    sum-=n-sw[sw.size()-1];

  }
    cout<<sum;
    }


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define ll long long
#define pi pair<int,int>
#define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n,a[300100];
string s;
ll dp[300100][3][3][2][2],ans;
ll Solve(int i,int j,int k,int st,int ok)
{
    if(i==n)
        return 0;
    ll &ret=dp[i][j][k][st][ok];
    if(ret+1)
        return ret;
    ret=0;
    ok|=(a[i]==st);
    if((a[i]==j||a[i]==k)&&ok)
        ret+=1+Solve(i+1,a[i],j,st,ok);
    else
        ret+=Solve(i+1,a[i],j,st,ok);
    return ret;
}
int main()
{
    go;
    memset(dp,-1,sizeof dp);
    cin>>n>>s;
    for(int i=0;i<n;i++)
        a[i]=s[i]-'A';
    for(int i=0;i<n;i++)
        ans+=Solve(i+1,a[i],2,a[i],0);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define ll long long
#define pi pair<int,int>
#define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n,a[300100];
string s;
ll dp[300100][3][3][2][2],ans;
ll Solve(int i,int j,int k,int st,int ok)
{
    if(i==n)
        return 0;
    ll &ret=dp[i][j][k][st][ok];
    if(ret+1)
        return ret;
    ret=0;
    ok|=(a[i]==st);
    if((a[i]==j||a[i]==k)&&ok)
        ret+=1LL+Solve(i+1,a[i],j,st,ok);
    else
        ret+=Solve(i+1,a[i],j,st,ok);
    return ret;
}
int main()
{
    go;
    memset(dp,-1,sizeof dp);
    cin>>n>>s;
    for(int i=0;i<n;i++)
        a[i]=s[i]-'A';
    for(int i=0;i<n;i++)
        ans+=Solve(i+1,a[i],2,a[i],0);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define pb push_back
#define pi pair<int,int>
#define pii pair<int,pi>
#define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
string s;
ll n,ans,dp[300100][2],dp2[300100][2];
ll Solve(int i,int j)
{
    if(i==n) return 0;
    if(dp[i][j]+1) return dp[i][j];
    if(s[i]==(j+'A')) return dp[i][j]=1;
    return dp[i][j]=Solve(i+1,j);
}
ll Solve2(int i,int j)
{
    if(i==n) return 0;
    if(dp2[i][j]+1) return dp2[i][j];
    if(s[i]==('A'+j)) return dp2[i][j]=0;
    return dp2[i][j]=1+Solve2(i+1,j);
}
int main()
{
    go;
    cin>>n>>s;
    ans=n*(n+1)/2-n;
    memset(dp,-1,sizeof dp);
    memset(dp2,-1,sizeof dp2);
    for(int i=0;i<n;i++)
    {
        if(s[i+1]==s[i])
        ans-=Solve(i,'B'-s[i]);
        else
        ans-=Solve2(i+1,s[i]-'A');
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define ll long long
#define pi pair<int,int>
#define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n,a[300100];
string s;
ll dp[300100][3][3][2][2],ans;
ll Solve(int i,int j,int k,int st,int ok)
{
    if(i==n)
        return 0;
    ll &ret=dp[i][j][k][st][ok];
    if(ret+1)
        return ret;
    ret=0;
    ok|=(a[i]==st);
    if((a[i]==j)&&ok)
        ret+=1LL+Solve(i+1,a[i],j,st,ok);
    else
        ret+=Solve(i+1,a[i],j,st,ok);
    return ret;
}
int main()
{
    go;
    memset(dp,-1,sizeof dp);
    cin>>n>>s;
    for(int i=0;i<n;i++)
        a[i]=s[i]-'A';
    for(int i=0;i<n;i++)
        ans+=Solve(i+1,a[i],2,a[i],0);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
const ll mod =   1e9+7;
int arr[N][2];
int main()
{  fast
ll n;
cin>>n;
string s;
cin>>s;arr[n+1][0]=arr[n+1][1]=n;
for(int i=n;i>=0;i--)
{
	arr[i][0]=arr[i+1][0];
	arr[i][1]=arr[i+1][1];
	if(s[i]=='A')arr[i][0]=i;
	else
	arr[i][1]=i;
}ll ans=0;
 ans=n*(n-1)/2;
int a=-1,b=-1;
for(int i=1;i<n;i++)
{
	if(s[i]=='A'&&s[i-1]=='B'||s[i]=='B'&&s[i-1]=='A')
	{
		if(s[i-1]=='A')
		{
			ans-=(i-1-b);ans-=(arr[i][0]-i);ans++;
		}else
		{
			ans-=(i-1-a);ans-=(arr[i][1]-i);ans++;
		}
}
if(s[i]=='A')
a=i;
else
b=i;
}cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
const ll mod =   1e9+7;
int arr[N][2];
int main()
{  fast
int n;
cin>>n;
string s;
cin>>s;arr[n+1][0]=arr[n+1][1]=n;
for(int i=n;i>=0;i--)
{
	arr[i][0]=arr[i+1][0];
	arr[i][1]=arr[i+1][1];
	if(s[i]=='A')arr[i][0]=i;
	else
	arr[i][1]=i;
}
ll ans=1ll*(((n-1)*(n))/2);
int a=-1,b=-1;
for(int i=1;i<n;i++)
{
	if(s[i]=='A'&&s[i-1]=='B'||s[i]=='B'&&s[i-1]=='A')
	{
		if(s[i-1]=='A')
		{
			ans-=(i-1-b);ans-=(arr[i][0]-i);ans++;
		}else
		{
			ans-=(i-1-a);ans-=(arr[i][1]-i);ans++;
		}
}
if(s[i]=='A')
a=i;
else
b=i;
}cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
const ll mod =   1e9+7;
int n;string s;
int main()
{  fast
cin>>n>>s;
int ans=n*(n-1)/2;
vector<int> v;
for(int i=0;i<n;)
{
	int x=i;
	while(x<n&&s[x]==s[i])
	x++;
	v.emplace_back(x-i);
	i=x;
}
for(int i=0;i+1<v.size();i++)
{
	ans-=(v[i]+v[i+1]);ans++;
}cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
const ll mod =   1e9+7;
ll n;string s;
int main()
{  fast
cin>>n>>s;
ll ans=n*(n-1)/2;
vector<ll> v;
for(int i=0;i<n;)
{
	int x=i;
	while(x<n&&s[x]==s[i])
	x++;
	v.emplace_back(x-i);
	i=x;
}
for(int i=0;i+1<v.size();i++)
{
	ans-=(v[i]+v[i+1]);ans++;
}cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
const ll mod =   1e9+7;
int arr[N][2];
int main()
{  fast
int n;
cin>>n;
string s;
cin>>s;arr[n+1][0]=arr[n+1][1]=n;
for(int i=n;i>=0;i--)
{
	arr[i][0]=arr[i+1][0];
	arr[i][1]=arr[i+1][1];
	if(s[i]=='A')arr[i][0]=i;
	else
	arr[i][1]=i;
}ll ans=0;

int a=-1,b=-1;
for(int i=1;i<n;i++)
{
	if(s[i]=='A'&&s[i-1]=='B'||s[i]=='B'&&s[i-1]=='A')
	{
		if(s[i-1]=='A')
		{
			ans+=(i-1-b);ans+=(arr[i][0]-i);ans--;
		}else
		{
			ans+=(i-1-a);ans+=(arr[i][1]-i);ans--;
		}
}
if(s[i]=='A')
a=i;
else
b=i;
}cout<<n*(n-1)/2-ans;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 3e5+10;
const ll mod =   1e9+7;
int arr[N][2];
int main()
{  fast
int n;
cin>>n;
string s;
cin>>s;arr[n+1][0]=arr[n+1][1]=n;
for(int i=n;i>=0;i--)
{
	arr[i][0]=arr[i+1][0];
	arr[i][1]=arr[i+1][1];
	if(s[i]=='A')arr[i][0]=i;
	else
	arr[i][1]=i;
}ll ans=0;
 ans=((n-1)*n)/2;
int a=-1,b=-1;
for(int i=1;i<n;i++)
{
	if(s[i]=='A'&&s[i-1]=='B'||s[i]=='B'&&s[i-1]=='A')
	{
		if(s[i-1]=='A')
		{
			ans-=(i-1-b);ans-=(arr[i][0]-i);ans++;
		}else
		{
			ans-=(i-1-a);ans-=(arr[i][1]-i);ans++;
		}
}
if(s[i]=='A')
a=i;
else
b=i;
}cout<<ans;
}


#include<iostream>
#include<string>
using namespace std;

string s;
bool chk[2];
int N;
long long ans;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 0; i < s.length(); i++) {
		if (chk[s[i] - 'A']) ans++;
		chk[s[i] - 'A'] = 1;
	}

	cout << ans*(ans+1)/2;
}


#include<iostream>
#include<string>
using namespace std;

string s;
bool chk[300001];
int N;
long long ans;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 1; i < s.length(); i++) {
		if (s[i] == s[i - 1]) chk[i] = 1;
	}
	for (int i = 1; i < s.length() - 1; i++) {
		if (s[i - 1] == s[i + 1]) chk[i + 1] = 1;
	}
	for (int i = 0; i < s.length(); i++) if (chk[i]) ans++;
	ans = ans * (ans + 1) / 2;
	cout << ans;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;

string s;
int x=1;
long long N;
long long ans;
vector<int> v;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 1; i < s.length(); i++) {
		if (s[i] == s[i - 1]) x++;
		else {
			v.push_back(x);
			x = 1;
		}
	}
	v.push_back(x);
	for (int i = 1; i < v.size(); i++) {
		ans += v[i - 1] + v[i]-1;
	}
	cout << N * (N - 1) / 2 - ans;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;

string s;
vector<int> v[2];
int x=1;
long long N;
long long ans;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 1; i < s.length(); i++) {
		if (s[i] == s[i - 1]) x++;
		else {
			ans += x;
			x = 1;
		}
	}
	if (x != N)ans += x - 1;
	cout << N * (N - 1) / 2 - ans;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;

string s;
vector<int> v[2];
int x=1;
long long N;
long long ans;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 1; i < s.length(); i++) {
		if (s[i] == s[i - 1]) x++;
		else {
			ans += x;
			x = 1;
		}
	}
	cout << N * (N - 1) / 2 - ans;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;

string s;
vector<int> v[2];
int N,arr[300001],x;
long long ans;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 0; i < s.length(); i++) {
		if (!v[s[i] - 'A'].empty()) {
			arr[i] = ++x;
		}
		else arr[i] = x;
		v[s[i] - 'A'].push_back(i);
	}
	for (int i = 1; i < v[0].size(); i++) {
		ans += x - arr[v[0][i]-1];
	}
	for (int i = 1; i < v[1].size(); i++) {
		ans += x - arr[v[1][i]-1];
	}
	cout << ans;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;

string s;
int x=1;
long long N;
long long ans;
vector<int> v;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 1; i < s.length(); i++) {
		if (s[i] == s[i - 1]) x++;
		else {
			v.push_back(x);
			x = 1;
		}
	}
	for (int i = 1; i < v.size(); i++) {
		ans += v[i - 1] + v[i];
	}
	cout << N * (N - 1) / 2 - ans;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;

string s;
vector<int> v[2];
int N,x=1;
long long ans;
int main() {
	cin >> N;
	cin >> s;
	for (int i = 1; i < s.length(); i++) {
		if (s[i] == s[i - 1]) x++;
		else {
			ans += x;
			x = 1;
		}
	}
	if (x != N) ans += x-1;
	cout << N * (N - 1) / 2 - ans;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <utility>
#include <deque>
#define int long long
using namespace std;
string s;
int n;
const int m = 3e5 + 9;
int cntA[m], cntB[m];
set<int> A, B;
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> s;
	s = '0' + s;
	int A_lt = 0, B_lt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'A')
		{
			cntA[i] = cntA[i - 1] + 1;
			cntB[i] = cntB[i - 1];
			A_lt = i;
			A.insert(i);
		}
		else
		{
			cntB[i] = cntB[i - 1] + 1;
			cntA[i] = cntA[i - 1];
			B_lt = i;
			B.insert(i);
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{

		if (i != A_lt && i != B_lt)
		{
			//cout << s[i];
			int nxt = s[i] == 'A' ? *(next(A.lower_bound(i))) : *(next(B.lower_bound(i)));
			ans += n - nxt + 1 - (s[i] == 'A' && cntB[nxt] - cntB[i] == 0 && cntB[n] - cntB[nxt] > 0) - (s[i] == 'B' && cntA[nxt] - cntA[i] == 0 &&  cntA[n] - cntA[nxt] > 0);
			//cout << n - nxt + 1 - (s[i] == 'A' && cntB[n] - cntB[nxt] > 0) - (s[i] == 'B' && cntA[n] - cntA[nxt] > 0) << endl;
		}
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
string s;
int n;
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> s;
	s = '0' + s;
	int A_fi = 0, A_lt = 0, B_fi = 0, B_lt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'A')
		{
			A_fi = A_fi == 0 ? i : A_fi;
			A_lt = i;
		}
		else
		{
			B_fi = B_fi == 0 ? i : B_fi;
			B_lt = i;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		if (i != A_lt && i != B_lt)
		{
			ans += n - i - (A_fi > i) - (B_fi > i) - (s[i + 1] != s[i] && A_fi <= i && B_fi <= i);
		}
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>

#define int long long
using namespace std;
string s;
int n;
const int m = 3e5 + 9;
int cntA[m], cntB[m];
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> s;
	s = '0' + s;
	int A_lt = 0, B_lt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'A')
		{
			cntA[i] = cntA[i - 1] + 1;
			cntB[i] = cntB[i - 1];
			A_lt = i;
		}
		else
		{
			cntB[i] = cntB[i - 1] + 1;
			cntA[i] = cntA[i - 1];
			B_lt = i;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		//cout << s[i];
		if (i != A_lt && i != B_lt)
		{
			ans += n - i - (s[i] == 'A' && cntB[n] - cntB[i] > 0) - (s[i] == 'B' && cntA[n] - cntA[i] > 0);
			//cout << n - i - (s[i] == 'A' && cntB[n] - cntB[i] > 0) - (s[i] == 'B' && cntA[n] - cntA[i] > 0) << endl;
		}
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#define int long long

using namespace std;
string s;
int n;
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> s;
	s = '0' + s;
	int A_fi = 0, A_lt = 0, B_fi = 0, B_lt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'A')
		{
			A_fi = A_fi == 0 ? i : A_fi;
			A_lt = i;
		}
		else
		{
			B_fi = B_fi == 0 ? i : B_fi;
			B_lt = i;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		if (i != A_lt && i != B_lt)
		{
			ans += n - i - (A_fi > i) - (B_fi > i) - (s[i + 1] != s[i] && A_fi <= i && B_fi <= i);
		}
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
string s;
int n;
const int m = 3e5 + 9;
int cntA[m], cntB[m];
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> s;
	s = '0' + s;
	int A_lt = 0, B_lt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'A')
		{
			cntA[i] = cntA[i - 1] + 1;
			cntB[i] = cntB[i - 1];
			A_lt = i;
		}
		else
		{
			cntB[i] = cntB[i - 1] + 1;
			cntA[i] = cntA[i - 1];
			B_lt = i;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		//cout << s[i];
		if (i != A_lt && i != B_lt)
		{
			ans += n - i - (s[i] == 'A' && cntB[n] - cntB[i] > 0) - (s[i] == 'B' && cntA[n] - cntA[i] > 0);
			//cout << n - i - (s[i] == 'A' && cntB[n] - cntB[i] > 0) - (s[i] == 'B' && cntA[n] - cntA[i] > 0) << endl;
		}
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}


#include<bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pp;

ll maxN=1000000000000000001;
ll cost[300001];
ll a[300001];
ll dp[300001][3];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;

    ll res=0;
    int a=-1,b=-1;
    for(int i=0;i<n;i++)
    {
        char ch;
        cin>>ch;
        if(ch=='A')
        {
            if(a!=-1&&b!=-1)
            res+=(min(a,b)+1);
            if(a!=-1&&b==-1)
            res+=i;
            a=i;
        }
        else
        {
            if(b!=-1&&a!=-1)
            res+=(min(a,b)+1);
            if(b!=-1&&a==-1)
            res+=i;
            b=i;
        }
    }
    cout<<res<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pp;

ll maxN=1000000000000000001;
ll cost[300001];
ll a[300001];
ll dp[300001][3];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;

    ll res=0;
    bool a=0,b=0;
    for(int i=0;i<n;i++)
    {
        char ch;
        cin>>ch;
        if(ch=='A')
        {
            if(b==1&&a==0)
            {
                a=1;
                continue;
            }
            if(b==1)
            res+=(i-1);
            else
            res+=(i);
            a=1;
        }
        else
        {
            if(a==1&&b==0)
            {
                b=1;
                continue;
            }
            if(a==1)
            res+=(i-1);
            else
            res+=(i);
            b=1;
        }
    }
    cout<<res<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pp;

ll maxN=1000000000000000001;
ll cost[300001];
ll a[300001];
ll dp[300001][3];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;

    ll res=0;
    int a=-1,b=-1;
    for(int i=0;i<n;i++)
    {
        char ch;
        cin>>ch;
        if(ch=='A')
        {
            if(a!=-1&&b!=-1)
            {
                if(b>a)
                {
                    res+=(a+1);
                }
                else
                res+=(i-1);
            }
            if(a!=-1&&b==-1)
            res+=i;
            a=i;
        }
        else
        {
            if(b!=-1&&a!=-1)
            {
                if(a>b)
                {
                    res+=(b+1);
                }
                else
                res+=(i-1);
            }
            if(b!=-1&&a==-1)
            res+=i;
            b=i;
        }

    }
    cout<<res<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pp;

ll maxN=1000000000000000001;
ll cost[300001];
ll a[300001];
ll dp[300001][3];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;

    ll res=0;
    int a=0,b=0;
    for(int i=0;i<n;i++)
    {
        char ch;
        cin>>ch;
        if(ch=='A')
        {
            if((a>=1&&b>=1))
            res+=(i-1);
            if(a>=1&&b==0)
            res+=i;
            a++;
        }
        else
        {
            if(a>=1&&b>=1)
            res+=(i-1);
            if(a==0&&b>=1)
            res+=i;
            b++;
        }
    }
    cout<<res<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pp;

ll maxN=1000000000000000001;
ll cost[300001];
ll a[300001];
ll dp[300001][3];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;

    ll res=0;
    bool a=0,b=0;
    for(int i=0;i<n;i++)
    {
        char ch;
        cin>>ch;
        if(ch=='A')
        {
            if(a==1&&b==1)
            res+=(i-1);
            if(a==1&&b==0)
            res+=i;
            a=1;
        }
        else
        {
            if(a==1&&b==1)
            res+=(i-1);
            if(a==0&&b==1)
            res+=i;
            b=1;
        }
    }
    cout<<res<<"\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(a) cerr << #a << " = " << (a) << endl;
#define fst first
#define snd second
#define sz(x) (int)(x).size()
#define mp(x, y) make_pair(x, y)
#define all(X) begin(X), end(X)
#define rall(X) rbegin(X), rend(X)

template<typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& x) { o << "(" << x.fst << ", " << x.snd << ")"; return o; }
template<typename T> ostream& operator<<(ostream& o, const vector<T>& x) { o << "["; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "]"; return o; }
template<typename T> ostream& operator<<(ostream& o, const set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }

typedef long long ll;
int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n; string A; cin >> n >> A;
  ll ans = (n * (n - 1)) / 2;

  for (int i = 0, j = 0; i < n; i = j) {
    j = i + 1;
    while (j < n and A[i] == A[j]) j++;

    if (i-1 >= 0) {
      ans -= j - i - 1;
    }

    if (j < n) {
      ans -= j - i;
    }
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(a) cerr << #a << " = " << (a) << endl;
#define fst first
#define snd second
#define sz(x) (int)(x).size()
#define mp(x, y) make_pair(x, y)
#define all(X) begin(X), end(X)
#define rall(X) rbegin(X), rend(X)

template<typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& x) { o << "(" << x.fst << ", " << x.snd << ")"; return o; }
template<typename T> ostream& operator<<(ostream& o, const vector<T>& x) { o << "["; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "]"; return o; }
template<typename T> ostream& operator<<(ostream& o, const set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }

typedef long long ll;
int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n; string A; cin >> n >> A;
  ll ans = (n * (n - 1)) / 2;
  debug(ans);

  for (int i = 0, j = 0; i < n; i = j) {
    j = i + 1;
    while (j < n and A[i] == A[j]) j++;

    if (i-1 >= 0) {
      assert(j-i-1>=0);
      ans -= j - i - 1;
    }

    if (j < n) {
      ans -= j - i;
    }

    debug(ans);
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(a) cerr << #a << " = " << (a) << endl;
#define fst first
#define snd second
#define sz(x) (int)(x).size()
#define mp(x, y) make_pair(x, y)
#define all(X) begin(X), end(X)
#define rall(X) rbegin(X), rend(X)

template<typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& x) { o << "(" << x.fst << ", " << x.snd << ")"; return o; }
template<typename T> ostream& operator<<(ostream& o, const vector<T>& x) { o << "["; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "]"; return o; }
template<typename T> ostream& operator<<(ostream& o, const set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }

typedef long long ll;
int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n; string A; cin >> n >> A;
  ll ans = (n * (n - 1)) / 2;

  debug(ans);
  for (int i = 0, j = 0; i < n; i = j) {
    j = i+1;
    if (j < n and A[i] != A[j]) {
      while (j < n and A[i+1] == A[j]) j++, ans--;
      j--;
    }
  }

  debug(ans);
  reverse(all(A));
  for (int i = 0, j = 0; i < n; i = j) {
    j = i+1;
    if (j < n and A[i] != A[j]) {
      while (j < n and A[i+1] == A[j]) j++, ans--;
      j--;
    }
  }

  debug(ans);
  for (int i = 0; i < n; i++) {
    if (i+1 < n and A[i] != A[i+1]) ans++;
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(a) cerr << #a << " = " << (a) << endl;
#define fst first
#define snd second
#define sz(x) (int)(x).size()
#define mp(x, y) make_pair(x, y)
#define all(X) begin(X), end(X)
#define rall(X) rbegin(X), rend(X)

template<typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& x) { o << "(" << x.fst << ", " << x.snd << ")"; return o; }
template<typename T> ostream& operator<<(ostream& o, const vector<T>& x) { o << "["; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "]"; return o; }
template<typename T> ostream& operator<<(ostream& o, const set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }

typedef long long ll;
int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  ll n; string A; cin >> n >> A;
  ll ans = (n * (n - 1)) / 2;
  for (int i = 0, j = 0; i < n; i = j) {
    j = i + 1;
    while (j < n and A[i] == A[j]) j++;

    if (i-1 >= 0) {
      ans -= j - i - 1;
    }

    if (j < n) {
      ans -= j - i;
    }
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define debug(a) cerr << #a << " = " << (a) << endl;
#define fst first
#define snd second
#define sz(x) (int)(x).size()
#define mp(x, y) make_pair(x, y)
#define all(X) begin(X), end(X)
#define rall(X) rbegin(X), rend(X)

template<typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& x) { o << "(" << x.fst << ", " << x.snd << ")"; return o; }
template<typename T> ostream& operator<<(ostream& o, const vector<T>& x) { o << "["; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "]"; return o; }
template<typename T> ostream& operator<<(ostream& o, const set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_set<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T> ostream& operator<<(ostream& o, const unordered_multiset<T>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_map<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }
template<typename T, typename U> ostream& operator<<(ostream& o, const unordered_multimap<T, U>& x) { o << "{"; int b = 0; for (auto& a : x) o << (b++ ? ", " : "") << a; o << "}"; return o; }

typedef long long ll;
int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  ll n; string A; cin >> n >> A;
  ll ans = (n * (n - 1)) / 2;
  for (int i = 0, j = 0; i < n; i = j) {
    j = i + 1;
    while (j < n and A[i] == A[j]) j++;

    if (i-1 >= 0) {
      assert(j-i-1>=0);
      ans -= j - i - 1;
    }

    if (j < n) {
      ans -= j - i;
    }

    debug(ans);
  }

  cout << ans << endl;
}


#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;

int const N=2e5+5, M=127+5, MOD=1e9+7, OO=0x3f3f3f3f;

int n;
string s;
unordered_set<int>st;
vector<int>B,A;

int main()
{
    FIO
//    freopen("input.txt","rt",stdin);
//    freopen("output.txt","wt",stdout);
    cin>>n>>s;
    int a=0,b=0,ca=0,cb=0;
    for(int i=0; i<n; i++)
    {
        if(i+3<n)
        {
            string ss=s.substr(i,4);
            if(ss=="ABBA" || ss=="BAAB")
                st.insert(i+4);
        }
        if(s[i]=='A')
        {
            ca++;
            A.push_back(cb);
            cb=0;
            b=0;
            if(a==0)
                a=1;
            else
                st.insert(i);
        }
        else
        {
            cb++;
            B.push_back(ca);
            ca=0;
            a=0;
            if(b==0)
                b=1;
            else
                st.insert(i);
        }
    }
    B.push_back(ca);
    A.push_back(cb);
    a=b=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            int mn=min(A[a],A[a+1]);
            a++;
            for(int j=1; j<=mn; j++)
                st.insert(i+j);
        }
        else
        {
            int mn=min(B[b],B[b+1]);
            b++;
            for(int j=1; j<=mn; j++)
                st.insert(i+j);
        }
    }
    ll sz=st.size();
    cout<<(sz*(sz+1))/2<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;

int const N=2e5+5, M=127+5, MOD=1e9+7, OO=0x3f3f3f3f;

int n;
string s;
unordered_set<int>st;
vector<int>B,A;

int main()
{
    FIO
//    freopen("input.txt","rt",stdin);
//    freopen("output.txt","wt",stdout);
    cin>>n>>s;
    int a=0,b=0,ca=0,cb=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            ca++;
            A.push_back(cb);
            cb=0;
            b=0;
            if(a==0)
                a=1;
            else
                st.insert(i);
        }
        else
        {
            cb++;
            B.push_back(ca);
            ca=0;
            a=0;
            if(b==0)
                b=1;
            else
                st.insert(i);
        }
    }
    B.push_back(ca);
    A.push_back(cb);
    a=b=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            int mn=min(A[a],A[a+1]);
            a++;
            for(int j=1; j<=mn; j++)
                st.insert(i+j);
        }
        else
        {
            int mn=min(B[b],B[b+1]);
            b++;
            for(int j=1; j<=mn; j++)
                st.insert(i+j);
        }
    }
    ll sz=st.size();
    cout<<(sz*(sz+1))/2<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;

int const N=2e5+5, M=127+5, MOD=1e9+7, OO=0x3f3f3f3f;

int n,tt;
string s;
ll ans;

int main()
{
    FIO
//    freopen("input.txt","rt",stdin);
//    freopen("output.txt","wt",stdout);
    cin>>n>>s;
    ans=n*(n-1)/2;
    tt=1;
    for(int i=1; i<n; i++)
    {
        if(s[i]==s[i-1])
            tt++;
        else
        {
            ans-=tt;
            tt=1;
        }
    }
    tt=1;
    for(int i=n-2; i>=0; i--)
    {
        if(s[i]==s[i+1])
            tt++;
        else
        {
            ans-=tt-1;
            tt=1;
        }
    }
    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;

int const N=2e5+5, M=127+5, MOD=1e9+7, OO=0x3f3f3f3f;

string s;
ll ans,n,tt;

int main()
{
    FIO
//    freopen("input.txt","rt",stdin);
//    freopen("output.txt","wt",stdout);
    cin>>n>>s;
    ans=n*(n-1)/2;
    tt=1;
    for(int i=1; i<n; i++)
    {
        if(s[i]==s[i-1])
            tt++;
        else
        {
            ans-=tt;
            tt=1;
        }
    }
    tt=1;
    for(int i=n-2; i>=0; i--)
    {
        if(s[i]==s[i+1])
            tt++;
        else
        {
            ans-=tt-1;
            tt=1;
        }
    }
    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;

int const N=2e5+5, M=127+5, MOD=1e9+7, OO=0x3f3f3f3f;

int n;
string s;
set<pair<int,int>>st;
vector<int>B,A;

int main()
{
    FIO
//    freopen("input.txt","rt",stdin);
//    freopen("output.txt","wt",stdout);
    cin>>n>>s;
    int a=0,b=0,ca=0,cb=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            ca++;
            A.push_back(cb);
            cb=0;
            b=0;
            if(a==0)
                a=1;
            else
                st.insert({i-1,i});
        }
        else
        {
            cb++;
            B.push_back(ca);
            ca=0;
            a=0;
            if(b==0)
                b=1;
            else
                st.insert({i-1,i});
        }
    }
    B.push_back(ca);
    A.push_back(cb);
    a=b=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            int mn=min(A[a],A[a+1]);
            a++;
            for(int j=1; j<=mn; j++)
                st.insert({i-j,i+j});
        }
        else
        {
            int mn=min(B[b],B[b+1]);
            b++;
            for(int j=1; j<=mn; j++)
                st.insert({i-j,i+j});
        }
    }
    ll sz=st.size();
    cout<<(sz*(sz+1))/2<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;

int const N=2e5+5, M=127+5, MOD=1e9+7, OO=0x3f3f3f3f;

int n;
string s;
unordered_set<int>st;
vector<int>B,A;

int main()
{
    FIO
//    freopen("input.txt","rt",stdin);
//    freopen("output.txt","wt",stdout);
    cin>>n>>s;
    int a=0,b=0,ca=0,cb=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            ca++;
            A.push_back(cb);
            cb=0;
            b=0;
            if(a==0)
                a=1;
            else
                st.insert(i);
        }
        else
        {
            cb++;
            B.push_back(ca);
            ca=0;
            a=0;
            if(b==0)
                b=1;
            else
                st.insert(i);
        }
    }
    B.push_back(ca);
    A.push_back(cb);
    a=b=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            int mn=min(A[a],A[a+1]);
            a++;
            if(mn!=0)
                st.insert(i+mn);
        }
        else
        {
            int mn=min(B[b],B[b+1]);
            b++;
            if(mn!=0)
                st.insert(i+mn);
        }
    }
    ll sz=st.size();
    cout<<(sz*(sz+1))/2<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;

int const N=2e5+5, M=127+5, MOD=1e9+7, OO=0x3f3f3f3f;

int n;
string s;
unordered_set<int>st;
ll ans;
vector<int>B,A;

int main()
{
    FIO
//    freopen("input.txt","rt",stdin);
//    freopen("output.txt","wt",stdout);
    cin>>n>>s;
    int a=0,b=0,ca=0,cb=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            ca++;
            A.push_back(cb);
            cb=0;
            b=0;
            if(a==0)
                a=1;
            else
            {
                ans++;
                st.insert(i);
            }
        }
        else
        {
            cb++;
            B.push_back(ca);
            ca=0;
            a=0;
            if(b==0)
                b=1;
            else
            {
                st.insert(i);
                ans++;
            }
        }
    }
    B.push_back(ca);
    A.push_back(cb);
    a=b=0;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='A')
        {
            int mn=min(A[a],A[a+1]);
            a++;
            for(int j=1; j<=mn; j++)
            {
                st.insert(i+j);
                ans++;
            }
        }
        else
        {
            int mn=min(B[b],B[b+1]);
            b++;
            for(int j=1; j<=mn; j++)
            {
                st.insert(i+j);
                ans++;
            }
        }
    }
    ll sz=st.size();
    cout<<(ans*(ans+1))/2<<"\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 100005
#define tc printf("Case %d: ", cs)
#define tcn printf("Case %d:\n", cs);
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);

#define dbg1(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;
#define dbg4(w,x, y, z) cerr << #w << " = " << w << ", " <<#x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if ( it != v.begin() ) os << ", ";
    os << *it;
  }
  return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if ( it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if ( it != v.begin() ) os << ", ";
    os << it -> first << " = " << it -> second ;
  }
  return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }
// Program showing a policy-based data structure.
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
// GNU link : https://goo.gl/WVDL6g
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
        tree_order_statistics_node_update>
        new_data_set;

/**___________________________________________________**/

int main()
{
  FASTIO
  ///*
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = (1ll * n * (n + 1)) / 2ll;
  vector<int> P[2];
  for (int i = 0; i < n; i++) {
    int c = s[i] - 'A';
    P[c].push_back(i);
  }

  ll bad = 0;
  for (int i = 0; i < n; i++) {
    int x = s[i] - 'A';
    int E = n;
    auto it = upper_bound(P[x].begin(), P[x].end(), i);
    if (it != P[x].end()) E = *it;
    bad += (E - i);
    it = lower_bound(P[x].begin(), P[x].end(), i);
    E = -1;
    if (it != P[x].begin()) {
      it--;
      E = *it;
    }
    bad += (i - E);
    if (i > 0 && s[i] != s[i - 1]) bad--;
  }
  bad -= n;
  cout << ans - bad << endl;
}