#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	long long n;
	string s;
	cin >> n >> s;
	long long res=n*(n-1)/2;
	long long sum=0;
	for(int i=0; i+1<n; i++){
		if(s[i]!=s[i+1]) sum++;
	}
	vector<int> a(1,-1), b(1,-1);
	for(int i=0; i<n; i++){
		if(s[i]=='A') a.push_back(i);
		else b.push_back(i);
	}
	a.push_back(n), b.push_back(n);
	for(int i=1; i+1<a.size(); i++){//B...BA
		sum+=max(a[i]-a[i-1]-2, 0);
	}
//	debug(sum)
	for(int i=1; i+1<a.size(); i++){//AB...B
		sum+=max(a[i+1]-a[i]-2, 0);
	}
//	debug(sum)
	for(int i=1; i+1<b.size(); i++){//A...AB
		sum+=max(b[i]-b[i-1]-2, 0);
	}
//	debug(sum)
	for(int i=1; i+1<b.size(); i++){//BA...A
		sum+=max(b[i+1]-b[i]-2, 0);
	}
//	debug(sum)
	res-=sum;
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	long long n;
	string s;
	cin >> n >> s;
	vector<int> v(1, 1);
	for(int i=1; i<n; i++){
		if(s[i]==s[i-1]) v.back()++;
		else v.push_back(1);
	}
	long long res=n*(n-1)/2;
	for(int i=1; i<v.size(); i++){
		res-=(v[i]+v[i-1]-1);
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
const int MAX=3*1e5;
vector<long long> tree(2*MAX, 0);
void update(int p, long long val){
	p+=MAX;
	tree[p]=max(tree[p], val);
	for(p/=2; p>=1; p/=2){
		tree[p]=max(tree[2*p], tree[2*p+1]);
	}
}
long long get(int l, int r){
	l+=MAX, r+=MAX;
	long long res=0;
	while(l<=r){
		if(l%2==1) res=max(res, tree[l++]);
		if(r%2==0) res=max(res, tree[r--]);
		l/=2, r/=2;
	}
	return res;
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	string s;
	cin >> n >> s;
	vector<int> r(n, 0);
	for(int i=0; i+1<n; i++){
		if(s[i]=='A'){
			if(s[i+1]=='A') r[i]=i+1;
			else{
				for(int j=i+1; j+1<n; j++){
					if(s[j]=='B' && s[j+1]=='A'){
						r[i]=j+1;
						i=j-1;
						break;
					}
					else r[j]=j+1;
				}
			}
		}
		else{
			if(s[i+1]=='B') r[i]=i+1;
			else{
				for(int j=i+1; j+1<n; j++){
					if(s[j]=='A' && s[j+1]=='B'){
						r[i]=j+1;
						i=j-1;
						break;
					}
					else r[j]=j+1;
				}
			}
		}
	}
	long long res=0;
	for(int i=0; i<n; i++){
		if(r[i]==0) continue;
		long long x=get(max(i-1, 0), r[i])+1;
		res+=x;
//		debug(i)
//		debug(x)
		update(r[i], x);
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	long long n;
	string s;
	cin >> n >> s;
	vector<int> a(1,-1), b(1, -1);
	for(int i=0; i<n; i++){
		if(s[i]=='A') a.push_back(i);
		else b.push_back(i);
	}
	a.push_back(n), b.push_back(n);
	long long res=n*(n-1)/2;
	for(int i=1; i+1<a.size(); i++){
		res-=(a[i]-a[i-1]-1)+(a[i+1]-a[i]-1);
	}
	for(int i=1; i+1<b.size(); i++){
		res-=max(b[i]-b[i-1]-2,0)+max(b[i+1]-b[i]-2,0);
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
const int MAX=3*1e5;
vector<long long> tree(2*MAX, 0);
void update(int p, long long val){
	p+=MAX;
	tree[p]=val;
	for(p/=2; p>=1; p/=2){
		tree[p]=max(tree[2*p], tree[2*p+1]);
	}
}
long long get(int l, int r){
	l+=MAX, r+=MAX;
	long long res=0;
	while(l<=r){
		if(l%2==1) res=max(res, tree[l++]);
		if(r%2==0) res=max(res, tree[r--]);
		l/=2, r/=2;
	}
	return res;
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	string s;
	cin >> n >> s;
	vector<int> r(n, 0);
	for(int i=0; i+1<n; i++){
		if(s[i]=='A'){
			if(s[i+1]=='A') r[i]=i+1;
			else{
				for(int j=i+2; j<n; j++){
					if(s[j]=='A'){
						r[i]=j;
						i=j-2;
						break;
					}
					else r[j-1]=j;
				}
			}
		}
		else{
			if(s[i+1]=='B') r[i]=i+1;
			else{
				for(int j=i+2; j<n; j++){
					if(s[j]=='B'){
						r[i]=j;
						i=j-2;
						break;
					}
					else r[j-1]=j;
				}
			}
		}
	}
	long long res=0;
	for(int i=0; i<n; i++){
		if(r[i]==0) continue;
		long long x=get(max(i-1, 0), r[i])+1;
		res+=x;
//		debug(i)
//		debug(x)
		update(r[i], x);
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
const int MAX=3*1e5;
vector<long long> tree(2*MAX, 0);
void update(int p, long long val){
	p+=MAX;
	tree[p]=val;
	for(p/=2; p>=1; p/=2){
		tree[p]=max(tree[2*p], tree[2*p+1]);
	}
}
long long get(int l, int r){
	l+=MAX, r+=MAX;
	long long res=0;
	while(l<=r){
		if(l%2==1) res=max(res, tree[l++]);
		if(r%2==0) res=max(res, tree[r--]);
		l/=2, r/=2;
	}
	return res;
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	string s;
	cin >> n >> s;
	vector<int> r(n, 0);
	for(int i=0; i+1<n; i++){
		if(s[i]=='A'){
			if(s[i+1]=='A') r[i]=i+1;
			else{
				for(int j=i+2; j<n; j++){
					if(s[j]=='A'){
						r[i]=j;
						i=j-2;
						break;
					}
					else r[j-1]=j;
				}
			}
		}
		else{
			if(s[i+1]=='B') r[i]=i+1;
			else{
				for(int j=i+2; j<n; j++){
					if(s[j]=='B'){
						r[i]=j;
						i=j-2;
						break;
					}
					else r[j-1]=j;
				}
			}
		}
	}
	long long res=0;
	for(int i=0; i<n; i++){
		if(r[i]==0) continue;
		long long x=get(max(i-1, 0), r[i]-1)+1;
		res+=x;
//		debug(i)
//		debug(x)
		update(r[i], x);
	}
	cout << res;
}


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

int main()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	vector <int> ab;
	unsigned long t = n;
	int count = 1;
	unsigned long ans = (t*(t-1))/2;
	char last = s[0];
	for(int i = 1; i < n; i++)
	{
		if(s[i] == last)
		{
			count++;
			continue;
		}
		else
		{
			ab.push_back(count);
			last = s[i];
			count = 1;
		}
	}
	ab.push_back(count);
	for(int i = 0; i < ab.size() - 1; i++)
	{
		int nxt = ab[i+1];
		long temp = ab[i] + nxt - 1;
		ans = ans - temp;
	}
	cout << ans << endl;
}


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

int main()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	vector <int> ab;
	unsigned long long t = n;
	int count = 1;
	unsigned long long ans = (t*(t-1))/2;
	char last = s[0];
	for(int i = 1; i < n; i++)
	{
		if(s[i] == last)
		{
			count++;
			continue;
		}
		else
		{
			ab.push_back(count);
			last = s[i];
			count = 1;
		}
	}
	ab.push_back(count);
	for(int i = 0; i < ab.size() - 1; i++)
	{
		int nxt = ab[i+1];
		long temp = ab[i] + nxt - 1;
		ans = ans - temp;
	}
	cout << ans << endl;
}


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

int main()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	vector <int> ab;
	long t = n;
	int count = 1;
	long ans = (t*(t-1))/2;
	char last = s[0];
	for(int i = 1; i < n; i++)
	{
		if(s[i] == last)
		{
			count++;
			continue;
		}
		else
		{
			ab.push_back(count);
			last = s[i];
			count = 1;
		}
	}
	ab.push_back(count);
	for(int i = 0; i < ab.size() - 1; i++)
	{
		int nxt = ab[i+1];
		long temp = ab[i] + nxt - 1;
		ans = ans - temp;
	}
	cout << ans << endl;
}


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

int main()
{
      int n,count=0,i,c=0;
      string s;
      cin>>n>>s;

      int sum=n*(n+1)/2;
      for(i=0;i<n-1;i++)
      {
            if(s[i]==s[i+1])
            {
                  count++;
            }
            else
            {

                  sum=sum-count-1;
                 //cout<<sum<<" "<<i<<endl;
                   count=0;

            }
      }
      count=0;
      for(i=n-1;i>0;i--)
      {
           if(s[i]==s[i-1])
            {
                  count++;
            }
            else
            {

                  sum=sum-count;

                   count=0;
                   break;

            }
      }

      cout<<sum-n <<endl;
      return 0;
}


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

int main()
{
      int n,count=1,i,c=0;
      string s;
      cin>>n>>s;

      int sum=n*(n+1)/2;
      for(i=0;i<n-1;i++)
      {
            if(s[i]==s[i+1])
            {
                  count++;
            }
            else
            {

                  sum=sum-count;

                   count=1;

            }
      }
      count=1;
      for(i=n-2;i>=0;i--)
      {
           if(s[i]==s[i+1])
            {
                  count++;
            }
            else
            {

                  sum=sum-(count-1);

                   count=1;

             }
      }

      cout<<sum-n <<endl;
      return 0;
}


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

int main()
{
      long long  n,count=1,i,c=1;
      string s;
      cin>>n>>s;

      long long sum=n*(n+1)/2 - n;
      for(i=0;i<n-1;i++)
      {
            if(s[i]==s[i+1])
            {
                  count++;
            }
            else
            {

                  sum=sum-count;

                   count=1;

            }
      }
      count=1;
      for(i=n-2;i>=0;i--)
      {
           if(s[i]==s[i+1])
            {
                  count++;
            }
            else
            {

                  sum=sum-(count-1);

                   count=1;

             }
      }

      cout<<sum<<endl;
      return 0;
}


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

int main()
{
      int t;
      cin>>t;
      while(t!=0)
      {
            int h,n,i;
            cin>>h>>n;
            int a[n];
            for(i=1;i<=n;i++)
            {
                  cin>>a[i];
            }
            a[n+1]=0;
            int count=0;
            for(i=2;i<=n;i++)
            {
                  if(h-a[i]>1)
                  {
                        h=a[i]+1;

                        i--;
                  }

                   else if(h-a[i+1]>2)
                  {
                        count++;
                        h=h-1;

                  }
                  else
                  {
                        i++;
                        h=a[i];

                  }

            }
            cout<<count<<endl;
            t--;

      }
      return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
int n;
char s[300005];
ll cntab[300005];
ll cntba[300005];
char s2[300005];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    ios::sync_with_stdio(0);
        cin>>n;
        cin>>s;
        for(int i=0;i<n;i++)s2[i]=s[n-i-1];
        ll ans=n*(n-1)/2;
        for(int i=0;i+1<n;i++)
        {
            cntab[i+1]=cntab[i];
            cntba[i+1]=cntba[i];
            if(s[i]=='A'&&s[i+1]=='B')
            {
               cntab[i+1]++;
            }
            else if(s[i]=='B'&&s[i+1]=='A')cntba[i+1]++;
        }
        ll x=cntab[n-1];ll y=cntba[n-1];
        ans-=x;ans-=y;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='A')
            {
                int j=i+1;
                if(j<n&&s[j]=='B')
                {
                    int cnt=0;
                    while(j+1<n&&s[j+1]=='B')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
            if(s[i]=='B')
            {
                int j=i+1;
                if(j<n&&s[j]=='A')
                {
                    int cnt=0;
                    while(j+1<n&&s[j+1]=='A')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            if(s2[i]=='A')
            {
                int j=i+1;
                if(j<n&&s2[j]=='B')
                {
                    int cnt=0;
                    while(j+1<n&&s2[j+1]=='B')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
            if(s2[i]=='B')
            {
                int j=i+1;
                if(j<n&&s2[j]=='A')
                {
                    int cnt=0;
                    while(j+1<n&&s2[j+1]=='A')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
        }
        cout<<ans<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
int n;
char s[300005];
ll cntab[300005];
ll cntba[300005];
char s2[300005];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    ios::sync_with_stdio(0);
        cin>>n;
        cin>>s;
        for(int i=0;i<n;i++)s2[i]=s[n-i-1];
        ll ans=n*(n-1)/2;
        for(int i=0;i+1<n;i++)
        {
            cntab[i+1]=cntab[i];
            cntba[i+1]=cntba[i];
            if(s[i]=='A'&&s[i+1]=='B')
            {
               cntab[i+1]++;
            }
            else if(s[i]=='B'&&s[i+1]=='A')cntba[i+1]++;
        }
        ll x=cntab[n-1];ll y=cntba[n-1];
        ans-=x*(x-1)/2;ans-=y*(y-1)/2;
        ans-=x;ans-=y;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='A')
            {
                int j=i+1;
                if(j<n&&s[j]=='B')
                {
                    int cnt=0;
                    while(j+1<n&&s[j+1]=='B')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
            if(s[i]=='B')
            {
                int j=i+1;
                if(j<n&&s[j]=='A')
                {
                    int cnt=0;
                    while(j+1<n&&s[j+1]=='A')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            if(s2[i]=='A')
            {
                int j=i+1;
                if(j<n&&s2[j]=='B')
                {
                    int cnt=0;
                    while(j+1<n&&s2[j+1]=='B')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
            if(s2[i]=='B')
            {
                int j=i+1;
                if(j<n&&s2[j]=='A')
                {
                    int cnt=0;
                    while(j+1<n&&s2[j+1]=='A')
                    {
                        cnt++;
                        j++;
                    }
                    ans-=cnt;
                }
            }
        }
        cout<<ans<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x)
#define pt(x) printf("%d\n",x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
using namespace std;
typedef long long ll;
const int mod =1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=7e5+50;
ll fpow(ll n,ll k,ll p=mod){ll r= 1;for(;k;k>>=1){if(k&1) r= r*n%p;n= n*n%p;}return r;}
ll inv(int n,int p=mod){return fpow(n,p-2,p);}
ll fact[maxn];
void init(ll n,ll p=mod){fact[0]=1;rep(i,1,n)fact[i]=fact[i-1]*i%p;}
ll C(ll n,ll m,ll p=mod){ll r= fact[n]; ll p1= inv(fact[m]); ll p2 = inv(fact[n-m]);return r*p1%p*p2%p;}
ll a[maxn];
char ch[maxn];
string s;
int cnt,len,ans=0;
int p[maxn*2];
int r[maxn];
void init()
{
	len = s.size();
	cnt=1;
	ch[0]='@';
	ch[cnt]='#';
	//cout<<len<<endl;
	for(int i=0;i<len;i++)
	{
		ch[++cnt] = s[i];
		ch[++cnt] ='#';
	}
//	cout<<ch<<endl;
}
void manacher()
{
	int pos=0,mx=0;
	for(int i=1;i<=cnt;i++)
	{
		if(i<mx)
		{p[i] = min(p[pos*2-i],mx-i);}
		else p[i] = 1;
		while(ch[i+p[i]]==ch[i-p[i]])
		{

			//cout<<i<<" "<<i+p[i]<<endl;
			if(ch[i+p[i]]==ch[i-p[i]])
				r[i+p[i]] = 1;
			p[i]++;
		}
		//cout<<r[i]<<" ";
		if(mx<i+p[i]) mx = i+p[i],pos=i;
		//ans = max(ans,p[i]-1);
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin>>n;
	cin>>s;
	init();
	manacher();
	long long k = 1,ans=0;
	for(int i=1;i<=cnt;i++)
	{
		if(i%2==0)
		{
			if(r[i])
			{
				ans = ans+k;
				k++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x)
#define pt(x) printf("%d\n",x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
using namespace std;
typedef long long ll;
const int mod =1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=7e5+50;
ll fpow(ll n,ll k,ll p=mod){ll r= 1;for(;k;k>>=1){if(k&1) r= r*n%p;n= n*n%p;}return r;}
ll inv(int n,int p=mod){return fpow(n,p-2,p);}
ll fact[maxn];
void init(ll n,ll p=mod){fact[0]=1;rep(i,1,n)fact[i]=fact[i-1]*i%p;}
ll C(ll n,ll m,ll p=mod){ll r= fact[n]; ll p1= inv(fact[m]); ll p2 = inv(fact[n-m]);return r*p1%p*p2%p;}
ll a[maxn];
char ch[maxn];
string s;
int cnt,len,ans=0;
int p[maxn*2];
int r[maxn*2];
void init()
{
	len = s.size();
	cnt=1;
	ch[0]='@';
	ch[cnt]='#';
	//cout<<len<<endl;
	for(int i=0;i<len;i++)
	{
		ch[++cnt] = s[i];
		ch[++cnt] ='#';
	}
//	cout<<ch<<endl;
}
void manacher()
{
	int pos=0,mx=0;
	for(int i=1;i<=cnt;i++)
	{
		if(i<mx)
		{p[i] = min(p[pos*2-i],mx-i);}
		else p[i] = 1;
		while(ch[i+p[i]]==ch[i-p[i]])
		{

			//cout<<i<<" "<<i+p[i]<<endl;
			if(ch[i+p[i]]==ch[i-p[i]])
				r[i+p[i]] = 1;
			p[i]++;
		}
		//cout<<r[i]<<" ";
		if(mx<i+p[i]) mx = i+p[i],pos=i;
		//ans = max(ans,p[i]-1);
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin>>n;
	cin>>s;
	init();
	manacher();
	long long k = 1,ans=0;
	for(int i=1;i<cnt;i++)
	{
		if(i%2==0)
		{
			if(r[i])
			{
				ans = ans+k;
				k++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin>>n;
	string s;
	cin>>s;
	ll len = s.size();
	ll cta=0,ctb=0;
	int fa=0,fb=0;
	ll ans = 0;
	for(int i=0;i<len;i++)
	{
		if(s[i]=='A')
		{
			cta++;
			fa = 1;
			if(cta==1&&fb) ans+= ctb;
			if(cta>1&&fb) ans++;
			ctb=0;
		}
		else
		{
			ctb++;
			fb = 1;
			if(ctb==1&&fa) ans += cta;
			if(ctb>1&&fa) ans++;
			cta=0;
		}
		//cout<<"ans= "<<ans<<endl;
	}
	printf("%I64d\n",len*(len-1)/2-ans);
	return 0;
 }


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	std::ios::sync_with_stdio(false);
	ll n;
	cin>>n;
	string s;
	cin>>s;
	ll res=n*(n-1)/2;
	int cnt=1;
	bool flag=false;
	for(int i=1;i<n;i++){
		if(s[i]==s[i-1])
			cnt++;
		else{
			if(flag)
				res-=cnt;//ABBBBB...
			flag=true;
			if(i<n-1)
				res-=cnt-1;//AAAAA...B
			cnt=1;
		}
	}
	if(cnt!=1 && flag)
		res-=cnt;
	cout<<res<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    std::ios::sync_with_stdio(false);
    ll n;
    string s;
    cin>>n>>s;
    ll res=n*(n-1)/2,cnt=1;
    bool flag=false;
    for(int i=1;i<n;i++){
        if(s[i]==s[i-1])
            cnt++;
        else{
            if(flag)
                res-=cnt-1;
            flag=true;
            res-=cnt;
            cnt=1;
        }
    }
    if(flag)
        res-=cnt-1;
    cout<<res<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	std::ios::sync_with_stdio(false);
	ll n;
	cin>>n;
	string s;
	cin>>s;
	ll res=n*(n-1)/2;
	int cnt=1;
	bool flag=false;
	for(int i=1;i<=n;i++){
		if(i!=n && s[i]==s[i-1])
			cnt++;
		else{
			if(flag)
				res-=cnt;//ABBBBB...
			flag=true;
			if(i<n-1)
				res-=cnt-1;//AAAAA...B
			cnt=1;
		}
	}
	cout<<res<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    std::ios::sync_with_stdio(false);
    ll n;
    string s;
    cin>>n>>s;
    ll res=n*(n-1)/2,cnt=1;
    bool flag=false;
    for(int i=1;i<n;i++){
        if(s[i]==s[i-1])
            cnt++;
        else{
            if(flag)
                res-=cnt-1;
            flag=true;
            if(i!=n-1)
                res-=cnt;
            cnt=1;
        }
    }
    if(flag)
        res-=cnt-1;
    cout<<res<<endl;
    return 0;
}


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

typedef long long ll;

const int N = 3e5 + 10;

ll n;
string s;
ll dp[N], dp2[N];
vector <ll> v;

int main() {
	ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n >> s;
	ll cnt = 1;
	for (ll i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) {
			dp[i] = dp[i - 1] + cnt;
			dp2[i] = cnt;
			if (!v.empty()) {
				ll p = v.size() - 1;
				dp[i] += dp2[v[p]];
				dp2[i] += dp2[v[p]];
			}
			cnt++;
		}
		else {
			cnt = 1;
			dp[i] = dp[i - 1];
			if (!v.empty()) {
				dp[i] += dp2[v[v.size() - 1]] + 1;
				dp2[i] += dp2[v[v.size() - 1]] + 1;
				if (v[v.size() - 1] > 0) {
					dp[i] += dp2[v[v.size() - 1] - 1];
					dp2[i] += dp2[v[v.size() - 1] - 1];
				}
			}
			v.push_back(i - 1);
		}
	}
	cout << dp[n - 1];
	return 0;
}


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

typedef long long ll;

const int N = 3e5 + 10;

ll n;
string s;
ll dp[N], dp2[N];
vector <ll> v;

int main() {
	ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n >> s;
	ll cnt = 1;
	for (ll i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) {
			dp2[i] = cnt;
			if (!v.empty()) {
				ll p = v.size() - 1;
				dp2[i] += dp2[v[p]];
			}
			cnt++;
		}
		else {
			cnt = 1;
			dp[i] = dp[i - 1];
			if (!v.empty()) {
				dp2[i] += dp2[v[v.size() - 1]] + 1;
				if (v[v.size() - 1] > 0) {
					dp2[i] += dp2[v[v.size() - 1] - 1];
				}
			}
			v.push_back(i - 1);
		}
	}
	ll ans = 0;
	for (int i = 0; i < n; i++)
		ans += dp2[i];
	cout << ans;
	return 0;
}


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

typedef long long ll;

const int N = 3e5 + 10;

int n;
string s;
ll dp[N], dp2[N];
vector <int> v;

int main() {
	ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n >> s;
	ll cnt = 1;
	for (int i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) {
			dp[i] = dp[i - 1] + cnt;
			dp2[i] = cnt;
			if (!v.empty()) {
				dp[i] += dp2[v[v.size() - 1]];
				dp2[i] += dp2[v[v.size() - 1]];
			}
			cnt++;
		}
		else {
			cnt = 1;
			dp[i] = dp[i - 1];
			if (!v.empty()) {
				dp[i] += dp2[v[v.size() - 1]] + 1;
				dp2[i] += dp2[v[v.size() - 1]] + 1;
				if (v[v.size() - 1] > 0) {
					dp[i] += dp2[v[v.size() - 1] - 1];
					dp2[i] += dp2[v[v.size() - 1] - 1];
				}
			}
			v.push_back(i - 1);
		}
	}
	cout << dp[n - 1];
	return 0;
}


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

typedef long long ll;

const int N = 3e5 + 10;

ll n;
string s;
vector <int> v;

int main() {
	ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n >> s;
	ll cnt = 1;
	for (int i = 1; i < n; i++) {
		if (s[i] == s[i - 1])
			cnt++;
		else {
			v.push_back(cnt);
			cnt = 1;
		}
	}
	v.push_back(cnt);
	ll ans = n * (n - 1) / 2;
	for (int i = 0;i < v.size() - 1; i++)
		ans -= v[i] + (v[i + 1] - 1);
	cout << ans;
	return 0;
}


// me & god
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 2e5 + 10, P = 205;

int t, n;
int s[N][P];

int main() {
	ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		cin >> n;
		vector <int> pos[P];
		for (int i = 0; i < n; i++) {
			int a;
			cin >> a;
			pos[a].push_back(i);
			for (int j = 1; j < P; j++)
				s[i][j] = s[i - 1][j];
			s[i][a]++;
		}
		for (int i = 1; i < 5; i++) {
			cout << i << " --> ";
			for (auto x: pos[i])
				cout << x << " ";
			cout << endl;
		}
	}
}


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

typedef long long ll;

const int N = 3e5 + 10;

ll n;
string s;
ll dp[N], dp2[N];
vector <ll> v;

int main() {
	ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n >> s;
	ll cnt = 1;
	for (int i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) {
			dp[i] = dp[i - 1] + cnt;
			dp2[i] = cnt;
			if (!v.empty()) {
				dp[i] += dp2[v[v.size() - 1]];
				dp2[i] += dp2[v[v.size() - 1]];
			}
			cnt++;
		}
		else {
			cnt = 1;
			dp[i] = dp[i - 1];
			if (!v.empty()) {
				dp[i] += dp2[v[v.size() - 1]] + 1;
				dp2[i] += dp2[v[v.size() - 1]] + 1;
				if (v[v.size() - 1] > 0) {
					dp[i] += dp2[v[v.size() - 1] - 1];
					dp2[i] += dp2[v[v.size() - 1] - 1];
				}
			}
			v.push_back(i - 1);
		}
	}
	cout << dp[n - 1];
	return 0;
}


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

typedef long long ll;

const int N = 3e5 + 10;

int n;
string s;
int dp[N], dp2[N];
vector <int> v;

int main() {
	ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n >> s;
	int cnt = 1;
	for (int i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) {
			dp[i] = dp[i - 1] + cnt;
			dp2[i] = cnt;
			if (!v.empty()) {
				dp[i] += dp2[v[v.size() - 1]];
				dp2[i] += dp2[v[v.size() - 1]];
			}
			cnt++;
		}
		else {
			cnt = 1;
			dp[i] = dp[i - 1];
			if (!v.empty()) {
				dp[i] += dp2[v[v.size() - 1]] + 1;
				dp2[i] += dp2[v[v.size() - 1]] + 1;
				if (v[v.size() - 1] > 0) {
					dp[i] += dp2[v[v.size() - 1] - 1];
					dp2[i] += dp2[v[v.size() - 1] - 1];
				}
			}
			v.push_back(i - 1);
		}
	}
	cout << dp[n - 1];
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int64_t long long
constexpr int64_t MOD=1e9+7;

int main() {
    ios_base::sync_with_stdio(0);
    int n;
    string s;
    cin>>n>>s;
    vector<int>a;
    int cur=1;
    int64_t all=0;
    for(int i=1;i<n;++i){
        if(s[i]==s[i-1]){
            ++cur;
        }else{
            a.push_back(cur);
            cur=1;
        }
        all+=i;
    }
    a.push_back(cur);
    if(a.size()==1){
        cout<<all<<endl;
        return 0;
    }
    int64_t rem=a[0]+a.back()-(a.size()-1);
    for(int i=1;i<a.size()-1;++i){
        rem+=a[i]*2;
    }
    cout<<(all-rem)<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int64_t long long
constexpr int64_t MOD=1e9+7;

int main() {
    ios_base::sync_with_stdio(0);
    int n;
    string s;
    cin>>n>>s;
    vector<int>a;
    int cur=1;
    int64_t all=0;
    for(int i=1;i<n;++i){
        if(s[i]==s[i-1]){
            ++cur;
        }else{
            a.push_back(cur);
            cur=1;
        }
        all+=i;
    }
    a.push_back(cur);
    if(a.size()==1){
        cout<<all<<endl;
        return 0;
    }
    int k=a.size()-1;
    int64_t rem=a[0]+a.back()-k;
    for(int i=1;i<a.size()-1;++i){
        rem+=a[i]*2;
    }
    cout<<(all-rem)<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int64_t long long
constexpr int64_t MOD=1e9+7;

int main() {
    ios_base::sync_with_stdio(0);
    int n;
    string s;
    cin>>n>>s;
    vector<int64_t>a;
    int cur=1;
    int64_t all=0;
    for(int i=1;i<n;++i){
        if(s[i]==s[i-1]){
            ++cur;
        }else{
            a.push_back(cur);
            cur=1;
        }
        all+=i;
    }
    a.push_back(cur);
    if(a.size()==1){
        cout<<all<<endl;
        return 0;
    }
    int64_t rem=a[0]+a.back()-(a.size()-1);
    for(int i=1;i<a.size()-1;++i){
        rem+=a[i]*2;
    }
    cout<<(all-rem)<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long

#define pb push_back
#define mp make_pair
#define inf 1e9
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=3e5+7;

int T;

char S[N];
int n;
int dp[N][4];
void solve(){
    scanf("%d",&n);
    scanf("%s",S+1);
    //memset(dp,0,sizeof(dp));
    rep(i,1,n){
        if(S[i]=='A'){
           if(S[i-1]==S[i]){
                dp[i+1][0]=dp[i][0]+1;
           }
           else{
                dp[i+1][0]=1;
           }
        }
        else{
            if(S[i-1]==S[i])dp[i+1][2]=dp[i][2]+1;
            else dp[i+1][2]=1;
        }
    }
    per(i,n,1){
        if(S[i]=='A'){
            if(S[i+1]==S[i])dp[i-1][1]=dp[i][1]+1;
            else dp[i-1][1]=1;
        }
        else{
            if(S[i+1]==S[i])dp[i-1][3]=dp[i][3]+1;
            else dp[i-1][3]=1;
        }
    }
    ll res=n*(n+1)/2;
    rep(i,1,n){
        if(S[i]=='A'){res-=dp[i][2]+dp[i][3]+1;}
        else {res-=dp[i][1]+dp[i][0]+1;if(dp[i][1])res++;if(dp[i][0])res++;}
        //cout<<res<<"\n";
    }
    cout<<res<<"\n";

}
int main(){
    T=1;
    rep(i,1,T)solve();

}


#include<bits/stdc++.h>
#define ll long long

#define pb push_back
#define mp make_pair
#define inf 1e9
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=3e5+7;

int T;

char S[N];
int n;
int dp[N][4];
void solve(){
    scanf("%d",&n);
    scanf("%s",S+1);
    //memset(dp,0,sizeof(dp));
    rep(i,1,n){
        if(S[i]=='A'){
           if(S[i-1]==S[i]){
                dp[i+1][0]=dp[i][0]+1;
           }
           else{
                dp[i+1][0]=1;
           }
        }
        else{
            if(S[i-1]==S[i])dp[i+1][2]=dp[i][2]+1;
            else dp[i+1][2]=1;
        }
    }
    per(i,n,1){
        if(S[i]=='A'){
            if(S[i+1]==S[i])dp[i-1][1]=dp[i][1]+1;
            else dp[i-1][1]=1;
        }
        else{
            if(S[i+1]==S[i])dp[i-1][3]=dp[i][3]+1;
            else dp[i-1][3]=1;
        }
    }
    ll res=1ll*n*(n+1)/2;
    rep(i,1,n){
        if(S[i]=='A'){res-=dp[i][2]+dp[i][3]+1;}
        else {res-=dp[i][1]+dp[i][0]+1;if(dp[i][1])res++;if(dp[i][0])res++;}
        //cout<<res<<"\n";
    }
    cout<<res<<"\n";

}
int main(){
    T=1;
    rep(i,1,T)solve();

}


#include<bits/stdc++.h>
#define ll long long

#define pb push_back
#define mp make_pair
#define inf 1e9
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=2e5+7;

int T;

char S[N];
int n;
int dp[N][4];
void solve(){
    scanf("%d",&n);
    scanf("%s",S+1);
    memset(dp,0,sizeof(dp));
    rep(i,1,n){
        if(S[i]=='A'){
           if(S[i-1]==S[i]){
                dp[i+1][0]=dp[i][0]+1;
           }
           else{
                dp[i+1][0]=1;
           }
        }
        else{
            if(S[i-1]==S[i])dp[i+1][2]=dp[i][2]+1;
            else dp[i+1][2]=1;
        }
    }
    per(i,n,1){
        if(S[i]=='A'){
            if(S[i+1]==S[i])dp[i-1][1]=dp[i][1]+1;
            else dp[i-1][1]=1;
        }
        else{
            if(S[i+1]==S[i])dp[i-1][3]=dp[i][3]+1;
            else dp[i-1][3]=1;
        }
    }
    ll res=n*(n+1)/2;
    rep(i,1,n){
        if(S[i]=='A'){res-=dp[i][2]+dp[i][3]+1;}
        else {res-=dp[i][1]+dp[i][0]+1;if(dp[i][1])res++;if(dp[i][0])res++;}
        //cout<<res<<"\n";
    }
    cout<<res<<"\n";

}
int main(){
    T=1;
    rep(i,1,T)solve();

}


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

      string s ;
    long long  n ,c=1,b,sol ;
   int main(){

          //freopen("a.in" ,"r",stdin);

       cin>>n>>s ;

         for(int i=1 ; i<n ; i++){
             if(s[i] == s[i-1])
                 c++ ;
             else{
             sol+=c ;
             c=1 ;
             b++;
             }

       }
        c=1;
       for(int i=n-2 ; i>=0 ; i--){
             if(s[i] == s[i+1])
                 c++ ;
             else
             sol+=c ,c=1;
             }

           cout<<n*(n-1)/2 - (sol-b) ;

     return 0;
   }


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

      string s ;
     int n ,c,b,sol ;
   int main(){

          //freopen("a.in" ,"r",stdin);

       cin>>n>>s ;

         for(int i=1 ; i<n ; i++){
             if(s[i] == s[i-1])
                 c++ ;
             else
             sol+=c ,c=1 ,b++;

       }
       for(int i=n-1 ; i>=0 ; i--){
             if(s[i] == s[i+1])
                 c++ ;
             else
             sol+=c ,c=1;
             }

           cout<<n*(n+1)/2 - (sol+b) ;

     return 0;
   }


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

      string s ;
     int n ,c,b,sol ;
   int main(){

       cin>>n>>s ;

         for(int i=1 ; i<n ; i++){
             if(s[i] == s[i-1])
                 c++ ;
             else
             sol+=c ,c=1;

       }
       for(int i=n-1 ; i>=0 ; i--){
             if(s[i] == s[i+1])
                 c++ ;
             else
             sol+=c ,c=1 ,b++;
             }

           cout<<n*(n+1)/2 - (sol+b) ;

     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 ff              first
#define ss              second
#define pb(x)           push_back(x)
#define mp              make_pair
#define sz(x)           (int)x.size()
#define mem(x ,y)       memset(x , y , sizeof x )
#define all(a )         a.begin() , a.end()
#define endl            "\n"
#define pii             pair<int,int>
#define piii            pair<int,pair<int,int > >
#define clr(x)          x.clear()
#define sl(x )          scanf("%I64d" ,&x)
#define sll(x,y )       scanf("%I64d%I64d" ,&x ,&y)
#define si(x )          scanf("%d" ,&x)
#define forn(i,x,y )    for(int i= x; i<=y ; i++ )
#define ford(i,x,y )    for(int i= x; i>=y ; i-- )
#define rep(i,n )       for(int i= 0; i<n ; i++ )
#define repi(i,n )      for(int i= 1; i<=n ; i++ )
#define repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )

#ifndef ONLINE_JUDGE
    #define dbg(args...)    do {   cout << #args << ' ' ;  print(args); } while(0); cerr<< endl ;
#else
    #define dbg(args...)   ;
#endif

typedef long long ll  ;
typedef unsigned long long ull  ;

ll  mod =  1e9+7, mod2 = 998244353 ;
int setbit(int n, int pos  ) { return n = n|(1LL<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1LL<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1LL<<pos))  ; }
template<typename T> void print(const T& v) {    cerr << v << ' ' ;}
template<typename T>T add(T x, T y, T mod = mod ){ x%= mod ;  y%= mod ;   return (x+y)%mod ;  }
template<typename T>T sub(T x, T y, T mod = mod ){ return ((x-y)%mod+mod )%mod;  }
template<typename T>T mul(T x, T y, T mod = mod ){ x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template<typename T1,typename... T2> void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
template<typename T> void dbg_a(T a[] ,int n=10 ) {cerr << "[ "; for(int i=0;i <= n ; i++ )cerr<<a[i]<<' ' ; cerr<< " ]" <<  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(int i=0; i<sz(v) ; i++ )  os << v[i] << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const map< T ,T> &Map ){os << "[ "; repit(it , Map ) os << "[" <<(*it).ff << ' ' << (*it).ss << "] "    ; return os << "]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const set< T > &Set  ){os << "[ "; repit(it , Set ) os <<  *it  << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const multiset< T > &Set) {os << "[ "; repit(it , Set )os <<  *it  << ' ' ; return os << " ]\n" ; }
template<class T> string to_str(T t){stringstream ss; ss<<t; return ss.str();}
// typedef tree<   int ,     // change type
//               null_type,
//               less_equal  < int  >  ,  // change type ..
//               rb_tree_tag,
//               tree_order_statistics_node_update> ordered_set;

    // order_of_key (k) : Number of items strictly smaller than k .
    // find_by_order(k) : K-th element in a set (counting from zero).

template < typename T >
T  bigmod(T x, T k ,T mod = mod) {
    if(k == 0 ) return (T)1;
    if(k&1  ) {
        return ( (x%mod)*bigmod(x,k-1, mod )%mod )%mod ;
    } else {
        T ret = bigmod(x,k/2  , mod ) ;
        ret %= mod ;
        return (ret*ret )%mod;
    }
}
template < typename T >
T  invmod( T x , T m = mod ) {
    return bigmod(x,m-2 , m) ;

}
#define  int  ll
const int maxn  = (int)3e5+123 ;
const ll inf  =  1e18 ; //  0xc0  ; //0x3f ; //
/////////////////////////////////////////////////////

int n ,m ;
int a[maxn  ] ;
string  s ;
void  solve() {
    int  x,y ,k, ans  = 0 ,q, id, curr  ,mx = 0   ;
    vector<int > v ;
    cin >> n >>   s ;

    v.pb(1)  ;
    repi(i, n-1 )  {
        if(s[i]==s[i-1] ) v.back()++ ;
        else v.pb( 1)  ;
    }

    ans  = (v[0]-1)*v[0]  ;
    ans /= 2LL ;
    int last  = v[0] ;
    repi(i,sz(v) -1) {
        x = v[i]  ;
        ans +=  ( x*(x-1))/2LL ;
        if(i-2>=0) ans += v[i-2]*x ;
        if(i==1)  {

            ans +=  (last-1)*(v[i]-1)  ;
        }
        last =  v[i]  ;
        v[i]+=v[i-1]  ;

    }
    // cout << v ;
    cout << ans  << endl;

}
signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
    ios :: sync_with_stdio(false);  cin.tie(0);  cout.tie(0) ;
    int t = 1  ;
    // cin  >>t ;
    while(t--) {
        solve() ;
    }
    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 ff              first
#define ss              second
#define pb(x)           push_back(x)
#define mp              make_pair
#define sz(x)           (int)x.size()
#define mem(x ,y)       memset(x , y , sizeof x )
#define all(a )         a.begin() , a.end()
#define endl            "\n"
#define pii             pair<int,int>
#define piii            pair<int,pair<int,int > >
#define clr(x)          x.clear()
#define sl(x )          scanf("%I64d" ,&x)
#define sll(x,y )       scanf("%I64d%I64d" ,&x ,&y)
#define si(x )          scanf("%d" ,&x)
#define forn(i,x,y )    for(int i= x; i<=y ; i++ )
#define ford(i,x,y )    for(int i= x; i>=y ; i-- )
#define rep(i,n )       for(int i= 0; i<n ; i++ )
#define repi(i,n )      for(int i= 1; i<=n ; i++ )
#define repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )

#ifndef ONLINE_JUDGE
    #define dbg(args...)    do {   cout << #args << ' ' ;  print(args); } while(0); cerr<< endl ;
#else
    #define dbg(args...)   ;
#endif

typedef long long ll  ;
typedef unsigned long long ull  ;

ll  mod =  1e9+7, mod2 = 998244353 ;
int setbit(int n, int pos  ) { return n = n|(1LL<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1LL<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1LL<<pos))  ; }
template<typename T> void print(const T& v) {    cerr << v << ' ' ;}
template<typename T>T add(T x, T y, T mod = mod ){ x%= mod ;  y%= mod ;   return (x+y)%mod ;  }
template<typename T>T sub(T x, T y, T mod = mod ){ return ((x-y)%mod+mod )%mod;  }
template<typename T>T mul(T x, T y, T mod = mod ){ x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template<typename T1,typename... T2> void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
template<typename T> void dbg_a(T a[] ,int n=10 ) {cerr << "[ "; for(int i=0;i <= n ; i++ )cerr<<a[i]<<' ' ; cerr<< " ]" <<  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(int i=0; i<sz(v) ; i++ )  os << v[i] << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const map< T ,T> &Map ){os << "[ "; repit(it , Map ) os << "[" <<(*it).ff << ' ' << (*it).ss << "] "    ; return os << "]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const set< T > &Set  ){os << "[ "; repit(it , Set ) os <<  *it  << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const multiset< T > &Set) {os << "[ "; repit(it , Set )os <<  *it  << ' ' ; return os << " ]\n" ; }
template<class T> string to_str(T t){stringstream ss; ss<<t; return ss.str();}
// typedef tree<   int ,     // change type
//               null_type,
//               less_equal  < int  >  ,  // change type ..
//               rb_tree_tag,
//               tree_order_statistics_node_update> ordered_set;

    // order_of_key (k) : Number of items strictly smaller than k .
    // find_by_order(k) : K-th element in a set (counting from zero).

template < typename T >
T  bigmod(T x, T k ,T mod = mod) {
    if(k == 0 ) return (T)1;
    if(k&1  ) {
        return ( (x%mod)*bigmod(x,k-1, mod )%mod )%mod ;
    } else {
        T ret = bigmod(x,k/2  , mod ) ;
        ret %= mod ;
        return (ret*ret )%mod;
    }
}
template < typename T >
T  invmod( T x , T m = mod ) {
    return bigmod(x,m-2 , m) ;

}
#define  int  ll
const int maxn  = (int)3e5+123 ;
const ll inf  =  1e18 ; //  0xc0  ; //0x3f ; //
/////////////////////////////////////////////////////

int n ,m ;
int a[maxn  ] ;
string  s ;
void  solve() {
    int  x,y ,k, ans  = 0 ,q, id, curr  ,mx = 0   ;
    vector<int > v ;
    cin >> n >>   s ;

    v.pb(1)  ;
    repi(i, n-1 )  {
        if(s[i]==s[i-1] ) v.back()++ ;
        else v.pb( 1)  ;
    }

    ans  = (v[0]-1)*v[0]  ;
    ans /= 2LL ;
    int last  = v[0] ;
    repi(i,sz(v) -1) {
        x = v[i]  ;
        ans +=  ( x*(x-1))/2LL ;
        if(i-2>=0) ans += v[i-2]*x ;
        if(i==1)  {

            ans +=  (last-1)*(v[i]-1)  ;
        }
        last =  v[i]  ;
        v[i]+=v[i-1]  ;

    }
    // cout << v ;
    cout << ans  << endl;

}
signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
    ios :: sync_with_stdio(false);  cin.tie(0);  cout.tie(0) ;
    int t = 1  ;
    // cin  >>t ;
    while(t--) {
        solve() ;
    }
    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 ff              first
#define ss              second
#define pb(x)           push_back(x)
#define mp              make_pair
#define sz(x)           (int)x.size()
#define mem(x ,y)       memset(x , y , sizeof x )
#define all(a )         a.begin() , a.end()
#define endl            "\n"
#define pii             pair<int,int>
#define piii            pair<int,pair<int,int > >
#define clr(x)          x.clear()
#define sl(x )          scanf("%I64d" ,&x)
#define sll(x,y )       scanf("%I64d%I64d" ,&x ,&y)
#define si(x )          scanf("%d" ,&x)
#define forn(i,x,y )    for(int i= x; i<=y ; i++ )
#define ford(i,x,y )    for(int i= x; i>=y ; i-- )
#define rep(i,n )       for(int i= 0; i<n ; i++ )
#define repi(i,n )      for(int i= 1; i<=n ; i++ )
#define repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )

#ifndef ONLINE_JUDGE
    #define dbg(args...)    do {   cout << #args << ' ' ;  print(args); } while(0); cerr<< endl ;
#else
    #define dbg(args...)   ;
#endif

typedef long long ll  ;
typedef unsigned long long ull  ;

ll  mod =  1e9+7, mod2 = 998244353 ;
int setbit(int n, int pos  ) { return n = n|(1LL<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1LL<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1LL<<pos))  ; }
template<typename T> void print(const T& v) {    cerr << v << ' ' ;}
template<typename T>T add(T x, T y, T mod = mod ){ x%= mod ;  y%= mod ;   return (x+y)%mod ;  }
template<typename T>T sub(T x, T y, T mod = mod ){ return ((x-y)%mod+mod )%mod;  }
template<typename T>T mul(T x, T y, T mod = mod ){ x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template<typename T1,typename... T2> void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
template<typename T> void dbg_a(T a[] ,int n=10 ) {cerr << "[ "; for(int i=0;i <= n ; i++ )cerr<<a[i]<<' ' ; cerr<< " ]" <<  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(int i=0; i<sz(v) ; i++ )  os << v[i] << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const map< T ,T> &Map ){os << "[ "; repit(it , Map ) os << "[" <<(*it).ff << ' ' << (*it).ss << "] "    ; return os << "]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const set< T > &Set  ){os << "[ "; repit(it , Set ) os <<  *it  << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const multiset< T > &Set) {os << "[ "; repit(it , Set )os <<  *it  << ' ' ; return os << " ]\n" ; }
template<class T> string to_str(T t){stringstream ss; ss<<t; return ss.str();}
// typedef tree<   int ,     // change type
//               null_type,
//               less_equal  < int  >  ,  // change type ..
//               rb_tree_tag,
//               tree_order_statistics_node_update> ordered_set;

    // order_of_key (k) : Number of items strictly smaller than k .
    // find_by_order(k) : K-th element in a set (counting from zero).

template < typename T >
T  bigmod(T x, T k ,T mod = mod) {
    if(k == 0 ) return (T)1;
    if(k&1  ) {
        return ( (x%mod)*bigmod(x,k-1, mod )%mod )%mod ;
    } else {
        T ret = bigmod(x,k/2  , mod ) ;
        ret %= mod ;
        return (ret*ret )%mod;
    }
}
template < typename T >
T  invmod( T x , T m = mod ) {
    return bigmod(x,m-2 , m) ;

}
#define  int  ll
const int maxn  = (int)3e5+123 ;
const ll inf  =  1e18 ; //  0xc0  ; //0x3f ; //
/////////////////////////////////////////////////////

int n ,m ;
int a[maxn  ] ;
string  s ;
void  solve() {
    int  x,y ,k, ans  = 0 ,q, id, curr  ,mx = 0   ;
    vector<int > v ;
    cin >> n >>   s ;

    v.pb(1)  ;
    repi(i, n-1 )  {
        if(s[i]==s[i-1] ) v.back()++ ;
        else v.pb( 1)  ;
    }

    ans  = (v[0]-1)*v[0]  ;
    ans /= 2LL ;

    repi(i,sz(v) -1) {
        x = v[i]  ;
        ans +=  ( x*(x-1))/2LL ;
        if(i-2>=0) ans += v[i-2]*x ;
        if(i==1)  {

            ans +=  (v[0]-1)*(v[1]-1)  ;
        }

        v[i]+=v[i-1]  ;

    }
    // cout << v ;
    cout << ans  << endl;

}
signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
    ios :: sync_with_stdio(false);  cin.tie(0);  cout.tie(0) ;
    int t = 1  ;
    // cin  >>t ;
    while(t--) {
        solve() ;
    }
    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 ff              first
#define ss              second
#define pb(x)           push_back(x)
#define mp              make_pair
#define sz(x)           (int)x.size()
#define mem(x ,y)       memset(x , y , sizeof x )
#define all(a )         a.begin() , a.end()
#define endl            "\n"
#define pii             pair<int,int>
#define piii            pair<int,pair<int,int > >
#define clr(x)          x.clear()
#define sl(x )          scanf("%I64d" ,&x)
#define sll(x,y )       scanf("%I64d%I64d" ,&x ,&y)
#define si(x )          scanf("%d" ,&x)
#define forn(i,x,y )    for(int i= x; i<=y ; i++ )
#define ford(i,x,y )    for(int i= x; i>=y ; i-- )
#define rep(i,n )       for(int i= 0; i<n ; i++ )
#define repi(i,n )      for(int i= 1; i<=n ; i++ )
#define repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )

#ifndef ONLINE_JUDGE
    #define dbg(args...)    do {   cout << #args << ' ' ;  print(args); } while(0); cerr<< endl ;
#else
    #define dbg(args...)   ;
#endif

typedef long long ll  ;
typedef unsigned long long ull  ;

ll  mod =  1e9+7, mod2 = 998244353 ;
int setbit(int n, int pos  ) { return n = n|(1LL<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1LL<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1LL<<pos))  ; }
template<typename T> void print(const T& v) {    cerr << v << ' ' ;}
template<typename T>T add(T x, T y, T mod = mod ){ x%= mod ;  y%= mod ;   return (x+y)%mod ;  }
template<typename T>T sub(T x, T y, T mod = mod ){ return ((x-y)%mod+mod )%mod;  }
template<typename T>T mul(T x, T y, T mod = mod ){ x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template<typename T1,typename... T2> void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
template<typename T> void dbg_a(T a[] ,int n=10 ) {cerr << "[ "; for(int i=0;i <= n ; i++ )cerr<<a[i]<<' ' ; cerr<< " ]" <<  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(int i=0; i<sz(v) ; i++ )  os << v[i] << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const map< T ,T> &Map ){os << "[ "; repit(it , Map ) os << "[" <<(*it).ff << ' ' << (*it).ss << "] "    ; return os << "]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const set< T > &Set  ){os << "[ "; repit(it , Set ) os <<  *it  << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const multiset< T > &Set) {os << "[ "; repit(it , Set )os <<  *it  << ' ' ; return os << " ]\n" ; }
template<class T> string to_str(T t){stringstream ss; ss<<t; return ss.str();}
// typedef tree<   int ,     // change type
//               null_type,
//               less_equal  < int  >  ,  // change type ..
//               rb_tree_tag,
//               tree_order_statistics_node_update> ordered_set;

    // order_of_key (k) : Number of items strictly smaller than k .
    // find_by_order(k) : K-th element in a set (counting from zero).

template < typename T >
T  bigmod(T x, T k ,T mod = mod) {
    if(k == 0 ) return (T)1;
    if(k&1  ) {
        return ( (x%mod)*bigmod(x,k-1, mod )%mod )%mod ;
    } else {
        T ret = bigmod(x,k/2  , mod ) ;
        ret %= mod ;
        return (ret*ret )%mod;
    }
}
template < typename T >
T  invmod( T x , T m = mod ) {
    return bigmod(x,m-2 , m) ;

}
#define  int  ll
const int maxn  = (int)3e5+123 ;
const ll inf  =  1e18 ; //  0xc0  ; //0x3f ; //
/////////////////////////////////////////////////////

int n ,m ;
int a[maxn  ] ;
string  s ;
void  solve() {
    int  x,y ,k, ans  = 0 ,q, id, curr  ,mx = 0   ;
    vector<int > v ;
    cin >> n >>   s ;

    v.pb(1)  ;
    repi(i, n-1 )  {
        if(s[i]==s[i-1] ) v.back()++ ;
        else v.pb( 1)  ;
    }

    ans  = (v[0]-1)*v[0]  ;
    ans /= 2LL ;
    int last  = v[0] ;
    repi(i,sz(v) -1) {
        x = v[i]  ;
        ans +=  ( x*(x-1))/2LL ;
        if(i-2>=0) ans += v[i-2]*x ;

        ans +=  (last-1)*(v[i]-1)  ;
        last =  v[i]  ;
        v[i]+=v[i-1]  ;

    }
    // cout << v ;
    cout << ans  << endl;

}
signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
    ios :: sync_with_stdio(false);  cin.tie(0);  cout.tie(0) ;
    int t = 1  ;
    // cin  >>t ;
    while(t--) {
        solve() ;
    }
    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 ff              first
#define ss              second
#define pb(x)           push_back(x)
#define mp              make_pair
#define sz(x)           (int)x.size()
#define mem(x ,y)       memset(x , y , sizeof x )
#define all(a )         a.begin() , a.end()
#define endl            "\n"
#define pii             pair<int,int>
#define piii            pair<int,pair<int,int > >
#define clr(x)          x.clear()
#define sl(x )          scanf("%I64d" ,&x)
#define sll(x,y )       scanf("%I64d%I64d" ,&x ,&y)
#define si(x )          scanf("%d" ,&x)
#define forn(i,x,y )    for(int i= x; i<=y ; i++ )
#define ford(i,x,y )    for(int i= x; i>=y ; i-- )
#define rep(i,n )       for(int i= 0; i<n ; i++ )
#define repi(i,n )      for(int i= 1; i<=n ; i++ )
#define repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )

#ifndef ONLINE_JUDGE
    #define dbg(args...)    do {   cout << #args << ' ' ;  print(args); } while(0); cerr<< endl ;
#else
    #define dbg(args...)   ;
#endif

typedef long long ll  ;
typedef unsigned long long ull  ;

ll  mod =  1e9+7, mod2 = 998244353 ;
int setbit(int n, int pos  ) { return n = n|(1LL<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1LL<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1LL<<pos))  ; }
template<typename T> void print(const T& v) {    cerr << v << ' ' ;}
template<typename T>T add(T x, T y, T mod = mod ){ x%= mod ;  y%= mod ;   return (x+y)%mod ;  }
template<typename T>T sub(T x, T y, T mod = mod ){ return ((x-y)%mod+mod )%mod;  }
template<typename T>T mul(T x, T y, T mod = mod ){ x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template<typename T1,typename... T2> void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
template<typename T> void dbg_a(T a[] ,int n=10 ) {cerr << "[ "; for(int i=0;i <= n ; i++ )cerr<<a[i]<<' ' ; cerr<< " ]" <<  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(int i=0; i<sz(v) ; i++ )  os << v[i] << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const map< T ,T> &Map ){os << "[ "; repit(it , Map ) os << "[" <<(*it).ff << ' ' << (*it).ss << "] "    ; return os << "]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const set< T > &Set  ){os << "[ "; repit(it , Set ) os <<  *it  << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const multiset< T > &Set) {os << "[ "; repit(it , Set )os <<  *it  << ' ' ; return os << " ]\n" ; }
template<class T> string to_str(T t){stringstream ss; ss<<t; return ss.str();}
// typedef tree<   int ,     // change type
//               null_type,
//               less_equal  < int  >  ,  // change type ..
//               rb_tree_tag,
//               tree_order_statistics_node_update> ordered_set;

    // order_of_key (k) : Number of items strictly smaller than k .
    // find_by_order(k) : K-th element in a set (counting from zero).

template < typename T >
T  bigmod(T x, T k ,T mod = mod) {
    if(k == 0 ) return (T)1;
    if(k&1  ) {
        return ( (x%mod)*bigmod(x,k-1, mod )%mod )%mod ;
    } else {
        T ret = bigmod(x,k/2  , mod ) ;
        ret %= mod ;
        return (ret*ret )%mod;
    }
}
template < typename T >
T  invmod( T x , T m = mod ) {
    return bigmod(x,m-2 , m) ;

}
#define  int  ll
const int maxn  = (int)3e5+123 ;
const ll inf  =  1e18 ; //  0xc0  ; //0x3f ; //
/////////////////////////////////////////////////////

int n ,m ;
int a[maxn  ] ;
string  s ;
void  solve() {
    int  x,y ,k, ans  = 0 ,q, id, curr  ,mx = 0   ;
    vector<int > v ;
    cin >> n >>   s ;

    v.pb(1)  ;
    repi(i, n-1 )  {
        if(s[i]==s[i-1] ) v.back()++ ;
        else v.pb( 1)  ;
    }

    ans  = (v[0]-1)*v[0]  ;
    ans /= 2LL ;
    int last  = v[0] ;
    repi(i,sz(v) -1) {
        x = v[i]  ;
        ans +=  ( x*(x-1))/2LL ;
        if(i-2>=0) ans += v[i-2]*x ;
        if(i==1)  {

            ans +=  (last-1)*(v[i]-1)  ;
        }
        last =  v[i]  ;
        v[i]+=v[i-1]  ;

    }
    // cout << v ;
    cout << ans  << endl;

}
signed main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
    ios :: sync_with_stdio(false);  cin.tie(0);  cout.tie(0) ;
    int t = 1  ;
    // cin  >>t ;
    while(t--) {
        solve() ;
    }
    return 0 ;
}


#include <cstdio>
#include <cstring>

const int maxn = 300000 + 5;
char S[maxn];

int main() {
	int n, st = 1;
	scanf("%d%s", &n, S);
	long long ans = n*(n-1)/2;
	while(S[st] == S[0]) st++;
	if(st != n) ans -= st;
	char c = S[st];
	for(long long i = st, last = i; i < n; i++) {
		if(c != S[i]) {
			ans -= (i - last)*2-1;
			last = i; c = S[i];
		}
		if(i == n-1) ans -= i-last;
	}

	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>

const int maxn = 300000 + 5;
char S[maxn];

int main() {
	int n, st = 1;
	scanf("%d%s", &n, S);
	long long ans = n*(n-1)/2;
	while(S[st] == S[0]) st++;
	if(st != n) ans -= st;
	char c = S[st];
	for(int i = st, last = i; i < n; i++) {
		if(c != S[i]) {
			ans -= (i - last)*2-1;
			last = i; c = S[i];
		}
		if(i == n-1) ans -= i-last;
	}

	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>

const int maxn = 300000 + 5;
char S[maxn];

int main() {
	long long n, st = 1;
	scanf("%lld%s", &n, S);
	long long ans = n*(n-1)/2;
	while(S[st] == S[0]) st++;
	if(st != n) ans -= st;
	char c = S[st];
	for(long long i = st, last = i; i < n; i++) {
		if(c != S[i]) {
			ans -= (i - last)*2-1;
			last = i; c = S[i];
		}
		if(i == n-1) ans -= i-last;
	}

	printf("%lld\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+50;
typedef long long ll;
const ll mod=1e9+7;

int a[maxn],b[maxn];

char s[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    ll ans=0;
    cin>>s+1;

    ans+=1LL*((n-1)*(n))/2;
    fill(a,a+1000,0);
    a[s[1]]=1;
    for(int i=2;i<=n;i++){
        ans-=i-a[s[i]]-1;
        a[s[i]]=i;
       // cout<<"i="<<i<<" ans="<<ans<<endl;
    }
    memset(a,0,sizeof(a));
    fill(a,a+1000,n+1);
    a[s[n]]=n;
    for(int i=n-1;i;i--){
        if(s[i]!=s[i+1])
        ans-=a[s[i]]-i-2;
        a[s[i]]=i;
    }

    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+50;
typedef long long ll;
const ll mod=1e9+7;

int a[maxn],b[maxn];

char s[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    ll ans=0;
    cin>>s+1;

    ans+=1LL*((n-1)*(n))/2;
    fill(a,a+1000,0);
    a[s[1]]=1;
    for(int i=2;i<=n;i++){
        if(s[i]!=s[i-1])
            ans-=i-a[s[i]]-1;
        a[s[i]]=i;
      //  cout<<"i="<<i<<" ans="<<ans<<endl;
    }
    memset(a,0,sizeof(a));
    fill(a,a+1000,n+1);
    a[s[n]]=n;
    for(int i=n-1;i;i--){
        if(s[i]!=s[i+1])
            ans-=a[s[i]]-i-2;
        a[s[i]]=i;
      //  cout<<"i="<<i<<" ans="<<ans<<endl;
    }

    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+50;
typedef long long ll;
const ll mod=1e9+7;

int a[maxn],b[maxn];

char s[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    ll ans=0;
    cin>>s+1;

    ans+=1LL*(n-1)*(n)/2;
    vector<int>v;
    for(int i=1;i<=n;i++){
        if(s[i]=='A'){
            int j=i;
            int num=1;
            while(j+1<=n&&s[j+1]=='A')j++,num++;
            i=j;
            v.push_back(num);
        }
        else{
            int j=i;
            int num=1;
            while(j+1<=n&&s[j+1]=='B')j++,num++;
            i=j;
            v.push_back(num);
        }
    }
    for(int i=1;i<v.size();i++){
        ans-=v[i]+v[i-1];ans++;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+50;
typedef long long ll;
const ll mod=1e9+7;

int a[maxn],b[maxn];

char s[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    ll ans=0;
    cin>>s+1;

    ans+=1LL*((n-1)*(n))/2;
    fill(a,a+1000,0);
    a[s[1]]=1;
    for(int i=2;i<=n;i++){
        ans-=i-a[s[i]]-1;
        a[s[i]]=i;
       // cout<<"i="<<i<<" ans="<<ans<<endl;
    }
    memset(a,0,sizeof(a));
    fill(a,a+1000,n+1);
    a[s[n]]=n;
    for(int i=n-1;i;i--){
        if(s[i]!=s[i+1])
        ans-=a[s[i]]-i-2;
        a[s[i]]=i;
    }

    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+50;
typedef long long ll;
const ll mod=1e9+7;

int a[maxn],b[maxn];

char s[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    ll ans=0;
    cin>>s+1;

    ans+=1LL*((n-1)*(n))/2;
    vector<int>v;
    for(int i=1;i<=n;i++){
        if(s[i]=='A'){
            int j=i;
            int num=1;
            while(j+1<=n&&s[j+1]=='A')j++,num++;
            i=j;
            v.push_back(num);
        }
        else{
            int j=i;
            int num=1;
            while(j+1<=n&&s[j+1]=='B')j++,num++;
            i=j;
            v.push_back(num);
        }
    }
    for(int i=1;i<v.size();i++){
        ans-=v[i]+v[i-1];ans++;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+50;
typedef long long ll;
const ll mod=1e9+7;

int a[maxn],b[maxn];

char s[maxn];
int main(){
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    ll ans=0;
    cin>>s+1;

    ans+=1LL*((n-1)*(n))/2;
    fill(a,a+1000,0);
    a[s[1]]=1;
    for(int i=2;i<=n;i++){
        if(a[i]!=a[i-1])
            ans-=i-a[s[i]]-1;
        a[s[i]]=i;
       // cout<<"i="<<i<<" ans="<<ans<<endl;
    }
    fill(a,a+1000,n+1);
    a[s[n]]=n;
    for(int i=n-1;i;i--){
        if(s[i]!=s[i+1])
            ans-=a[s[i]]-i-2;
        a[s[i]]=i;
    }

    cout<<ans<<endl;
    return 0;
}


#include<iostream>//正难则反，统计badstring的数量
#include<cstdio>
using namespace std;
long long ans,n,cnt,a[300010],tot;
char s[300010];
int main()
{
	cin>>n;//观察可得，badstring只有四种：ABBBB...;BAAAA...;...BBBBA;...AAAAB
	cin>>s;
	for(int i=0;i<n;i++)
	{
		a[cnt]=1;
		while(i<n&&s[i]==s[i+1])
		{
			i++;
			a[cnt]++;//统计本字符到下一个字符间的距离
		}
		cnt++;//统计AB或BA出现的次数，这部分重复
	}//-2*n:可以认为每个字符都是左badstirng的结尾和右badstring的开头
	ans=n*(n-1)/2-2*n+(cnt-1)+a[0]+a[cnt-1];//a[0]内的字符无左，a[cnt-1]内的字符无右
	printf("%lld",ans);
	return 0;
 }


#include<iostream>
#include<cstdio>
using namespace std;
long long ans,n,cnt,a[300010];
char s[300010];
int main()
{
	cin>>n;
	cin>>s+1;
	for(int i=1;i<=n;i++)
	{
		a[cnt]=1;
		while(i<=n&&s[i]==s[i+1])
		{
			i++;
			cnt++;
		}
		cnt++;
	}
	ans=n*(n-1)/2-(cnt-1);
	printf("%lld",ans);
	return 0;
 }


#include<iostream>
#include<cstdio>
using namespace std;
long long ans,n,cnt,a[300010],tot;
char s[300010];
int main()
{
	cin>>n;
	cin>>s;
	for(int i=0;i<n;i++)
	{
		a[cnt]=1;
		while(i<n&&s[i]==s[i+1])
		{
			i++;
			a[cnt]++;
		}
		cnt++;
	}
	ans=n*(n-1)/2-2*n+(cnt-1)+a[0]+a[cnt-1];
	printf("%lld",ans);
	return 0;
 }


#include <bits/stdc++.h>
#define ll  long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sc second
#define fr first

using namespace std;

char s[1001000];

int main()  {
        ll n;
        scanf("%lld",&n);
        scanf("%s",s);
        ll res = n*(n-1)/2;
        int pra = 0,prb = 0,r =0;
        for(int i=0 ;i <n ;i ++){
            if(i && s[i] != s[i-1])
                r = 1;
            if(s[i] == 'A'){
                res -= i-pra;
                pra = i+1;
            }
            else{
                res -= i-prb;
                prb = i+1;
            }
        }
        res += r;
        if(pra)
        res -= n-pra;
        if(prb)
        res -= n-prb;
        printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
#define ll  long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sc second
#define fr first

using namespace std;

char s[1001000];

int main()  {
    ll n;
    scanf("%lld",&n);
    scanf("%s",s);
    ll res = n*(n-1)/2;
    int pra = 0,prb = 0,r =0;
    for(int i=0 ;i <n ;i ++){
        if(i && s[i] != s[i-1])
            r = 1;
        if(s[i] == 'A'){
            if(pra ==0 && i-pra > 1)res++;
            res -= i-pra;
            pra = i+1;
        }
        else{

            if(prb ==0 && i-prb > 1)res++;
            res -= i-prb;
            prb = i+1;
        }
    }
    if(pra)
        res -= n-pra;
    if(prb)
        res -= n-prb;
    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
#define ll  long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sc second
#define fr first

using namespace std;

char s[1001000];

int main()  {
    ll n;
    scanf("%lld",&n);
    scanf("%s",s);
    ll res = n*(n-1)/2;
    int pra = 0,prb = 0,r =0;
    for(int i=0 ;i <n ;i ++){
        if(i && s[i] != s[i-1])
            r = 1;
        if(s[i] == 'A'){
            res -= i-pra;
            pra = i+1;
        }
        else{
            res -= i-prb;
            prb = i+1;
        }
    }
    if(n > 1 && s[n-2] != s[n-1])r = 0;
    res += r;
    if(pra)
        res -= n-pra;
    if(prb)
        res -= n-prb;
    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
#define ll  long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sc second
#define fr first

using namespace std;

char s[1001000];

int main()  {
    ll n;
    scanf("%lld",&n);
    scanf("%s",s);
    ll res = n*(n-1)/2;
    for(int i=1 ;i <n ; i++){
        res += s[i] != s[i-1];
    }

    for(int i=0 ;i <n ;){
        int j=i+1;
        int r =0;
        for( ; j < n ; j++){
            if(s[i] == s[j])break;
            r++;
        }

        if(r)j--;
        i = j;
        res -= r;
    }
    reverse(s,s+n);
    for(int i=0 ;i <n ;){
        int j=i+1;
        int r =0;
        for( ; j < n ; j++){
            if(s[i] == s[j])break;
            r++;
        }
        if(r)j--;
        i = j;
        res -= r;
    }

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define lc id<<1
#define rc id<<1|1

const int maxn = 3e5 + 10;

typedef long long ll;

using namespace std;

ll n, ans;
int p[maxn];
char s[maxn];

ll read(){
	ll x = 0, f = 1; char ch;
	do{ch = getchar(); if (ch == '-') f = -1;}while(ch < '0' || ch > '9');
	do{x = x * 10 + ch - 48; ch = getchar();}while(ch >= '0' && ch <= '9');
	return x * f;
}
int main(){
	n = read();
	scanf("%s", s+1);
	ll j = 1;
	for (ll i = 1;i <= n;i = j + 1){
		j = i;
		while(j + 1 <= n && s[j+1] == s[i]) j ++;
		if (j - i + 1 == 2) ans ++;
		else if (j - i + 1 > 2) ans += ((1 + j - i) * (j - i) / 2);
	}
	bool fir = true;
	for (ll i = 2;i < n;i ++){
		if (s[i-1] != s[i] && s[i] != s[i+1]){
			if (fir){
				ans += ((n - i) * (i - 1));
				fir = false;
			}
			else ans += (n - i);
		}
	}
	int cnt = 0;
	for (int i = 1;i < n;i ++) if (s[i] == s[i+1]) p[++cnt] = i;
	int N = cnt;
	cnt = 0;
	ll p1 = 0, p2 = 0;
	for (ll l = 1, r = 1;l <= n;){
		if (r < l) r = l;
		while(r + 1 <= n && (r-1 < l || s[r+1] == s[r] || s[r-1] == s[r])) r ++;
		while(p1 < l && cnt + 1 <= N) p1 = p[++cnt];
		int num = cnt;
		while(num + 1 <= N && (p2-1 <= p1 || s[p2-1] == s[p1])) p2 = p[++num]+1;
		if (p2 <= r && p1 >= l && s[p1] != s[p2-1]) ans += ((r - p2 + 1) * (p1 - l + 1));
		else{
			l ++;
			continue;
		}
		if (p1 + 1 <= l) l ++;
		else l = p1 + 1;
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define lc id<<1
#define rc id<<1|1

const int maxn = 3e5 + 10;

typedef long long ll;

using namespace std;

ll n, ans;
int p[maxn];
char s[maxn];

ll read(){
	ll x = 0, f = 1; char ch;
	do{ch = getchar(); if (ch == '-') f = -1;}while(ch < '0' || ch > '9');
	do{x = x * 10 + ch - 48; ch = getchar();}while(ch >= '0' && ch <= '9');
	return x * f;
}
int main(){
	n = read();
	scanf("%s", s+1);
	ll j = 1;
	for (ll i = 1;i <= n;i = j + 1){
		j = i;
		while(j + 1 <= n && s[j+1] == s[i]) j ++;
		if (j - i + 1 == 2) ans ++;
		else if (j - i + 1 > 2) ans += ((1 + j - i) * (j - i) / 2);
	}
	bool fir = true;
	for (ll i = 2;i < n;i ++){
		if (s[i-1] != s[i] && s[i] != s[i+1]){
			if (fir){
				ans += ((n - i) * (i - 1));
				fir = false;
			}
			else ans += (n - i);
		}
	}
	int cnt = 0;
	for (int i = 1;i < n;i ++) if (s[i] == s[i+1]) p[++cnt] = i;
	int N = cnt;
	cnt = 0;
	ll p1 = 0, p2 = 0;
	for (ll l = 1, r = 1;l <= n;){
		if (r < l) r = l;
		while(r + 1 <= n && (r-1 < l || s[r+1] == s[r] || s[r-1] == s[r])) r ++;
		while(p1 < l && cnt + 1 <= N) p1 = p[++cnt];
		int num = cnt;
		while(num + 1 <= N && (p2-1 <= p1 || s[p2-1] == s[p1])) p2 = p[++num]+1;
		if (p2 <= r && p1 >= l && s[p1] != s[p2-1]) ans += ((r - p2 + 1) * (p1 - l + 1));
		if (p1 + 1 == l) l ++;
		else l = p1 + 1;
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

const int maxn = 3e5 + 10;

typedef long long ll;

using namespace std;

ll n, ans, cnt;
char s[maxn];

int main(){
    cin >> n >> s+1;
    ans = n * (n + 1) / 2 - n;
    cnt = 1;
    for (int i = 2;i <= n;i ++){
    	if (s[i] == s[i-1]) cnt ++;
    	else{
    		ans -= cnt - 1;
    		cnt = 1;
    	}
    }
    cnt = 1;
	for (int i = n-1;i >= 1;i --){
		if (s[i] == s[i+1]) cnt ++;
		else{
			ans -= cnt;
			cnt = 1;
		}
	}
	cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

const int maxn = 3e5 + 10;

using namespace std;

int n, ans, cnt;
char s[maxn];

int main(){
    cin >> n >> s+1;
    ans = n * (n + 1) / 2 - n;
    cnt = 1;
    for (int i = 2;i <= n;i ++){
    	if (s[i] == s[i-1]) cnt ++;
    	else{
    		ans -= cnt - 1;
    		cnt = 1;
    	}
    }
    cnt = 1;
	for (int i = n-1;i >= 1;i --){
		if (s[i] == s[i+1]) cnt ++;
		else{
			ans -= cnt;
			cnt = 1;
		}
	}
	cout << ans << endl;
    return 0;
}


#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<utility>
#include<ostream>
#include<istream>
typedef long long ll;
using namespace std;
#define mod 1e9+7
#define PI acos(-1.0)
const ll maxn=3*1e5;
const int mx=(1<<20)+99;
ll n,m,ans,aans;
char a[maxn+10];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    //int t;
    //cin>>t;
    //for(int o=1;o<=t;o++)
    //{
        cin>>n;
        for(int i=1;i<=n;i++)
        cin>>a[i];
        ans=0;
        int numa,numb;
        for(int len=2;len<=n;len++)
        {
            for(int i=1,j=i+len-1;j<=n;i++,j++)
            {
                numa=numb=0;
                for(int k=i;k<=j;k++)
                {
                    if(a[k]=='A') numa++;
                    else numb++;
                }
                if(numa==1)
                {
                    if(a[i]=='A'||a[j]=='A') continue;
                    else ans++;
                }
                else if(numb==1)
                {
                    if(a[i]=='B'||a[j]=='B') continue;
                    else ans++;
                }
                else ans++;
            }
        }
        cout<<ans<<endl;
    //}
    return 0;
}
/*
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}

ll cal(ll a,ll b)//a^b
{
    ll ans=1;
    while(b)
    {
        if(b%2)
        ans=ans*a%mod;
        b/=2;
        a=a*a%mod;
    }
    return ans;
}

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    ll d=egcd(b,a%b,x,y);
    ll x1=x;
    x=y;
    y=x1-(a/b)*y;
    return d;
}
*/


#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<utility>
#include<ostream>
#include<istream>
typedef long long ll;
using namespace std;
#define mod 1e9+7
#define PI acos(-1.0)
const ll maxn=3*1e5;
const int mx=(1<<20)+99;
ll n,m,ans,aans;
char a[maxn+10];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    //int t;
    //cin>>t;
    //for(int o=1;o<=t;o++)
    //{
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        ans=n*(n-1)/2; aans=1;
        for(int i=2;i<=n;i++)
        {
            if(a[i]==a[aans]) continue;
            ans-=i-aans;
            if(aans!=1) ans-=i-aans-1;
            aans=i;
        }
        if(aans!=1) ans-=n-aans;
        cout<<ans<<endl;
    //}
    return 0;
}
/*
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}

ll cal(ll a,ll b)//a^b
{
    ll ans=1;
    while(b)
    {
        if(b%2)
        ans=ans*a%mod;
        b/=2;
        a=a*a%mod;
    }
    return ans;
}

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    ll d=egcd(b,a%b,x,y);
    ll x1=x;
    x=y;
    y=x1-(a/b)*y;
    return d;
}
*/


#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<utility>
#include<ostream>
#include<istream>
typedef long long ll;
using namespace std;
#define mod 1e9+7
#define PI acos(-1.0)
const ll maxn=3*1e5;
const int mx=(1<<20)+99;
ll n,m,ans,aans;
char a[maxn+10];
struct lll{
    ll aa,bb;
} b[maxn+10];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    //int t;
    //cin>>t;
    //for(int o=1;o<=t;o++)
    //{
        cin>>n;
        b[0].aa=b[0].bb=0;
        for(int i=1;i<=n;i++)
        {
            b[i].aa=b[i].bb=0;
            cin>>a[i];
            if(a[i]=='A') { b[i].aa=b[i-1].aa+1; b[i].bb=b[i-1].bb; }
            else { b[i].bb=b[i-1].bb+1; b[i].aa=b[i-1].aa; }
        }
        ans=0;
        for(int len=2;len<=n;len++)
        {
            for(int i=1,j=i+len-1;j<=n;i++,j++)
            {
                if(b[j].aa-b[i-1].aa==1)
                {
                    if(a[i]=='A'||a[j]=='A') continue;
                    else ans++;
                }
                else if(b[j].bb-b[i-1].bb==1)
                {
                    if(a[i]=='B'||a[j]=='B') continue;
                    else ans++;
                }
                else ans++;
            }
        }
        cout<<ans<<endl;
    //}
    return 0;
}
/*
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}

ll cal(ll a,ll b)//a^b
{
    ll ans=1;
    while(b)
    {
        if(b%2)
        ans=ans*a%mod;
        b/=2;
        a=a*a%mod;
    }
    return ans;
}

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    ll d=egcd(b,a%b,x,y);
    ll x1=x;
    x=y;
    y=x1-(a/b)*y;
    return d;
}
*/


#include <bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

const ll MXN = 3e5 + 7;
const ll MOD = 1e9 + 7;
const ld eps = 1e-9;

void free()
{
    freopen("txt.in", "r", stdin);
    freopen("txt.out", "w", stdout);
}

void kastiel()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int n;

ll ans;

char s[MXN];

int main()
{
    ///free();
    kastiel();
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> s[i];
    }
    int cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= n; i++){
        if(s[i] == 'A'){
            cnt1++;
            ans += cnt2;
            cnt2 = 0;
        }
        else {
            cnt2++;
            ans += cnt1;
            cnt1 = 0;
        }
    }
    cnt1 = 0;
    cnt2 = 0;
    for(int i = n; i >= 1; i--){
        if(s[i] == 'A'){
            cnt1++;
            if(cnt2 > 1){
                ans += cnt2 - 1;
            }
            cnt2 = 0;
        }
        else {
            cnt2++;
            if(cnt1 > 1){
                ans += cnt1 - 1;
            }
            cnt1 = 0;
        }
    }
    ll sum = 0;
    sum += (n - 1);
    sum *= n;
    sum /= 2;
    cout << sum - ans;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

const ll MXN = 3e5 + 7;
const ll MOD = 1e9 + 7;
const ld eps = 1e-9;

void free()
{
    freopen("txt.in", "r", stdin);
    freopen("txt.out", "w", stdout);
}

void kastiel()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int n;

ll ans;

char s[MXN];

string q, t;

map <string, int> m;

int main()
{
    ///free();
    kastiel();
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> s[i];
    }
    int cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= n; i++){
        q += s[i];
        t += s[i];
        if(s[i] == 'A'){
            cnt1++;
            if(m[q] == 0){
                ans += cnt2;
                m[q]++;
                q.clear();
            }
            cnt2 = 0;
        }
        if(s[i] == 'B'){
            if(m[t] == 0){
                ans += cnt1;
                m[t]++;
                t.clear();
            }
            cnt1 = 0;
            cnt2++;
        }
    }
    cnt1 = 0;
    cnt2 = 0;
    t.clear();
    q.clear();
    for(int i = n; i >= 1; i--){
        q += s[i];
        t += s[i];
        if(s[i] == 'A'){
            cnt1++;
            if(m[q] == 0){
                ans += cnt2;
                m[q]++;
                q.clear();
            }
            cnt2 = 0;
        }
        else {
            if(m[t] == 0){
                ans += cnt1;
                m[t]++;
                t.clear();
            }
            cnt2++;
            cnt1 = 0;
        }
    }
    ll sum = 0;
    sum += (n - 1);
    sum *= n;
    sum /= 2;
    if(ans != 0){
        cout << sum - ans + 1;
        return 0;
    }
    cout << sum - ans;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

const ll MXN = 3e5 + 7;
const ll MOD = 1e9 + 7;
const ld eps = 1e-9;

void free()
{
    freopen("txt.in", "r", stdin);
    freopen("txt.out", "w", stdout);
}

void kastiel()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int n;

ll ans;

char s[MXN];

string q, t;

map <string, int> m;

int main()
{
    ///free();
    kastiel();
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> s[i];
    }
    int cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= n; i++){
        q += s[i];
        t += s[i];
        if(s[i] == 'A'){
            cnt1++;
            if(m[q] == 0){
                ans += cnt2;
                m[q]++;
                q.clear();
            }
            cnt2 = 0;
        }
        if(s[i] == 'B'){
            if(m[t] == 0){
                ans += cnt1;
                m[t]++;
                t.clear();
            }
            cnt1 = 0;
            cnt2++;
        }
    }
    cnt1 = 0;
    cnt2 = 0;
    t.clear();
    q.clear();
    for(int i = n; i >= 1; i--){
        q += s[i];
        t += s[i];
        if(s[i] == 'A'){
            cnt1++;
            if(m[q] == 0){
                ans += cnt2;
                m[q]++;
                q.clear();
            }
            cnt2 = 0;
        }
        else {
            if(m[t] == 0){
                ans += cnt1;
                m[t]++;
                t.clear();
            }
            cnt2++;
            cnt1 = 0;
        }
    }
    ll sum = ((n - 1) * n) / 2;
    if(ans != 0){
        cout << sum - ans + 1;
        return 0;
    }
    cout << sum - ans;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

const ll MXN = 3e5 + 7;
const ll MOD = 1e9 + 7;
const ld eps = 1e-9;

void free()
{
    freopen("txt.in", "r", stdin);
    freopen("txt.out", "w", stdout);
}

void kastiel()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int n;

ll ans;

char s[MXN];

string q, t;

map <string, int> m;

int main()
{
    ///free();
    kastiel();
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> s[i];
    }
    int cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= n; i++){
        q += s[i];
        t += s[i];
        if(s[i] == 'A'){
            cnt1++;
            if(m[q] == 0){
                ans += cnt2;
                m[q]++;
                q.clear();
            }
            cnt2 = 0;
        }
        if(s[i] == 'B'){
            if(m[t] == 0){
                ans += cnt1;
                m[t]++;
                t.clear();
            }
            cnt1 = 0;
            cnt2++;
        }
    }
    cnt1 = 0;
    cnt2 = 0;
    t.clear();
    q.clear();
    for(int i = n; i >= 1; i--){
        q += s[i];
        t += s[i];
        if(s[i] == 'A'){
            cnt1++;
            if(m[q] == 0){
                ans += cnt2;
                m[q]++;
                q.clear();
            }
            cnt2 = 0;
        }
        else {
            if(m[t] == 0){
                ans += cnt1;
                m[t]++;
                t.clear();
            }
            cnt2++;
            cnt1 = 0;
        }
    }
    if(ans != 0){
        cout << ((n - 1) * n) / 2 - ans + 1;
        return 0;
    }
    cout << ((n - 1) * n) / 2 - ans;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

const ll MXN = 3e5 + 7;
const ll MOD = 1e9 + 7;
const ld eps = 1e-9;

void free()
{
    freopen("txt.in", "r", stdin);
    freopen("txt.out", "w", stdout);
}

void kastiel()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int n;

ll ans;

char s[MXN];

int main()
{
    ///free();
    kastiel();
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> s[i];
    }
    int cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= n; i++){
        if(s[i] == 'A'){
            cnt1++;
            if(cnt2 > 1){
                ans += cnt2;
                ans--;
            }
            cnt2 = 0;
        }
        if(s[i] == 'B'){
            if(cnt1 > 1){
                ans += cnt1;
                ans--;
            }
            cnt1 = 0;
            cnt2++;
        }
    }
    cnt1 = 0;
    cnt2 = 0;
    for(int i = n; i >= 1; i--){
        if(s[i] == 'A'){
            cnt1++;
            if(cnt2 > 1){
                ans += cnt2;
                ans--;
            }
            cnt2 = 0;
        }
        else {
            if(cnt1 > 1){
                ans += cnt1;
                ans--;
            }
            cnt2++;
            cnt1 = 0;
        }
    }
    for(int i = 1; i < n; i++){
        if(s[i] == 'A' && s[i + 1] == 'B'){
            ans++;
        }
    }
    ll sum = 0;
    sum += (n - 1);
    sum *= n;
    sum /= 2;
    cout << sum - ans;
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef ONLINE_JUDGE
const int N = 1e6+50;
#else
const int N = 1e2+10;
#endif

int n;
char s[N];
ll ans;
int vis[N];
int dp[N];

int main() {
	scanf("%d%s",&n,s+1);
	ans = n*(n-1)/2;
	REP(i,1,n) {
		auto p = string(s+i,s+i+3);
		if (p=="ABB") ans-=n-i-1;
		if (p=="AAB") ans-=i;
		if (p=="BAA") ans-=n-i-1;
		if (p=="BBA") ans-=i;
		p = string(s+i,s+i+2);
		if (p=="AB") --ans;
		if (p=="BA") --ans;
	}
	//左ABB, 右BBA
	REP(i,3,n) {
		dp[i] = dp[i-1];
		auto p = string(s+i-2,s+i+1);
		if (p=="ABB") ++dp[i];
		if (p=="BBA") ans+=dp[i];
		if (p=="AAB") ans+=dp[i];
	}
	REP(i,3,n) {
		dp[i] = dp[i-1];
		auto p = string(s+i-2,s+i+1);
		if (p=="BAA") ++dp[i];
		if (p=="BBA") ans+=dp[i];
		if (p=="AAB") ans+=dp[i];
	}
	printf("%lld\n", ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#define int ll
const int N = 1e6+10;
int n;
char s[N];
ll ans;
int vis[N];
int dp[N];

signed main() {
	scanf("%lld%s",&n,s+1);
	ans = (ll)n*(n-1)/2;
	REP(i,1,n) {
		auto p = string(s+i,s+i+3);
		if (p=="ABB") ans-=n-i-1;
		if (p=="AAB") ans-=i;
		if (p=="BAA") ans-=n-i-1;
		if (p=="BBA") ans-=i;
		p = string(s+i,s+i+2);
		if (p=="AB") --ans;
		if (p=="BA") --ans;
	}
	REP(i,0,n+4) dp[i]=0;
	//左ABB, 右BBA
	REP(i,3,n) {
		dp[i] = dp[i-1];
		auto p = string(s+i-2,s+i+1);
		if (p=="ABB") ++dp[i];
		if (p=="BBA") ans+=dp[i];
		if (p=="AAB") ans+=dp[i];
	}
	REP(i,0,n+4) dp[i]=0;
	REP(i,3,n) {
		dp[i] = dp[i-1];
		auto p = string(s+i-2,s+i+1);
		if (p=="BAA") ++dp[i];
		if (p=="BBA") ans+=dp[i];
		if (p=="AAB") ans+=dp[i];
	}
	printf("%lld\n", ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef ONLINE_JUDGE
const int N = 1e6+50;
#else
const int N = 1e2+10;
#endif

int n;
char s[N];
ll ans;
int vis[N];
int dp[N];

int main() {
	scanf("%d%s",&n,s+1);
	ans = (ll)n*(n-1)/2;
	REP(i,1,n) {
		auto p = string(s+i,s+i+3);
		if (p=="ABB") ans-=n-i-1;
		if (p=="AAB") ans-=i;
		if (p=="BAA") ans-=n-i-1;
		if (p=="BBA") ans-=i;
		p = string(s+i,s+i+2);
		if (p=="AB") --ans;
		if (p=="BA") --ans;
	}
	//左ABB, 右BBA
	REP(i,3,n) {
		dp[i] = dp[i-1];
		auto p = string(s+i-2,s+i+1);
		if (p=="ABB") ++dp[i];
		if (p=="BBA") ans+=dp[i];
		if (p=="AAB") ans+=dp[i];
	}
	REP(i,3,n) {
		dp[i] = dp[i-1];
		auto p = string(s+i-2,s+i+1);
		if (p=="BAA") ++dp[i];
		if (p=="BBA") ans+=dp[i];
		if (p=="AAB") ans+=dp[i];
	}
	printf("%lld\n", ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef ONLINE_JUDGE
const int N = 1e6+50;
#else
const int N = 1e2+10;
#endif

int n, vis[N], dp[N];
char s[N];

int main() {
	scanf("%d%s",&n,s+1);
	REP(i,1,n) {
		if (s[i]==s[i+1]) vis[i]=vis[i+1]=1;
		if (s[i]==s[i+2]) vis[i]=vis[i+1]=vis[i+2]=1;
	}
	ll ans = 0;
	REP(i,1,n+1) {
		if (vis[i]) dp[i]=dp[i-1]+1;
		else ans += (ll)dp[i-1]*(dp[i-1]-1)/2;
	}
	printf("%lld\n",ans);
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

#ifdef ONLINE_JUDGE
const int N = 1e6+50;
#else
const int N = 1e2+10;
#endif

int n;
char s[N];
pii a[N];

int main() {
	scanf("%d%s",&n,s+1);
	int cnt = 0;
	REP(i,1,n) {
		if (!cnt) a[++cnt]=pii(s[i],1);
		else {
			if (s[i]==a[cnt].x) ++a[cnt].y;
			else a[++cnt]=pii(s[i],1);
		}
	}
	ll ans = (ll)n*(n-1)/2;
	//减去AAB和BBA
	REP(i,1,cnt) ans-=a[i-1].y;
	//减去ABBBB
	REP(i,1,cnt) ans-=a[i+1].y;
	//加上AB
	ans += cnt-1;
	printf("%lld\n",ans);
}


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

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    vector<pair<int,int>> x,z;
    int i,p,q,l,r,t1,t2,j,k;
    long long int count=0;
    cin>>p>>q>>l>>r;
    for(i=0;i<p;i++)
    {
        cin>>t1>>t2;
        z.push_back(pair<int,int>(t1,t2));
    }
    for(i=0;i<q;i++)
    {
        cin>>t1>>t2;
        x.push_back(pair<int,int>(t1,t2));
    }
    int check;
    for(k=l;k<=r;k++)
    {
        check=0;
        for(i=0;i<q;i++)
        {
            for(j=0;j<p;j++)
            {
                if((x[i].second + k <= z[j].second && x[i].second + k >= z[j].first) || (x[i].first+k >= z[j].first && x[i].first+k <= z[j].second))
                {
                    check=1;
                }
            }
        }
        if(check)
            count++;
    }
    cout<<count<<endl;
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    vector<pair<int,int>> x,z;
    int i,p,q,l,r,t1,t2,j,k;
    long long int count=0;
    cin>>p>>q>>l>>r;
    for(i=0;i<p;i++)
    {
        cin>>t1>>t2;
        x.push_back(pair<int,int>(t1,t2));
    }
    for(i=0;i<q;i++)
    {
        cin>>t1>>t2;
        z.push_back(pair<int,int>(t1,t2));
    }
    int check=0;
    for(k=l;k<=r;k++)
    {
        for(j=0;j<q;j++)
        {
            for(i=0;i<p;i++)
            {
                check=0;
                if(((z[j].first+k >= x[i].first) && z[j].first+k <= x[i].second) || (z[i].second+k <= x[i].second && z[i].second+k >= x[i].first) )
                {
                    check=1;
                }
            }
            if(check)
                count++;
        }
    }
    cout<<count<<endl;
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    vector<pair<int,int>> x,z;
    int i,p,q,l,r,t1,t2,j,k;
    long long int count=0;
    cin>>p>>q>>l>>r;
    for(i=0;i<p;i++)
    {
        cin>>t1>>t2;
        x.push_back(pair<int,int>(t1,t2));
    }
    for(i=0;i<q;i++)
    {
        cin>>t1>>t2;
        z.push_back(pair<int,int>(t1,t2));
    }
    int check=0;
    for(k=l;k<=r;k++)
    {
        check=0;
        for(j=0;j<q;j++)
        {
            for(i=0;i<p;i++)
            {
                if(((z[j].first+k >= x[i].first) && z[j].first+k <= x[i].second) || (z[j].second+k <= x[i].second && z[j].second+k >= x[i].first) )
                {
                    check=1;
                }
            }
        }
        if(check)
            count++;
    }
    cout<<count<<endl;
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    vector<pair<int,int>> x,z;
    int i,p,q,l,r,t1,t2,j,k;
    long long int count=0;
    cin>>p>>q>>l>>r;
    for(i=0;i<p;i++)
    {
        cin>>t1>>t2;
        z.push_back(pair<int,int>(t1,t2));
    }
    for(i=0;i<q;i++)
    {
        cin>>t1>>t2;
        x.push_back(pair<int,int>(t1,t2));
    }
    int check;
    for(i=0;i<q;i++)
    {
        for(k=l;k<=r;k++)
        {
            check=0;
            for(j=0;j<p;j++)
            {
                if((x[i].second + k <= z[j].second && x[i].second + k >= z[j].first) || (x[i].first+k >= z[j].first && x[i].first+k <= z[j].second))
                {
                    check=1;
                }
            }
            if(check)
                count++;
        }
    }
    cout<<count<<endl;
    return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    int p,q,l,r,i,j,k;
    int a[50],b[50],c[50],d[50];
    cin>>p>>q>>l>>r;
    for(i=0;i<p;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(i=0;i<q;i++)
    {
        cin>>c[i]>>d[i];
    }
    int check=0,count=0;
    for(k=l;k<=r;k++)
    {
        check=0;
        for(i=0;i<p;i++)
        {
            for(j=0;j<q;j++)
            {
                int from,to;
                from=max(a[i],c[j]+k);
                to=min(b[i],d[j]+k);
                if(from<=to)
                    check=1;
            }
        }
        if(check)
            count++;
    }
    cout<<count<<endl;
    return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < (int) b; i++)
#define F0R(i, a) FOR (i, 0, a)
#define ROF(i, a, b) for(int i = a; i >= (int) b; i--)
#define R0F(i, a) ROF(i, a, 0)
#define GO(i, a) for (auto i : a)

#define eb emplace_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vpii> vvpii;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef vector<pi64> vpi64;

const int N = 1e3 + 500;

int ci, cf;
vpii interval (55);
vi cnt (N);
vi must (N);

bool solve () {
  int cur = 0, chk = 0;
  F0R (i, N) {
    cur += cnt[i];
    chk += must[i];
    if (cur > 0 && chk > 0) return true;
  }
  return false;
}

int main() {
  int p, q, l, r;
  scanf ("%d %d %d %d", &p, &q, &l, &r);
  F0R (i, p) {
    int st, et;
    scanf ("%d %d", &st, &et);
    ++cnt[st];
    --cnt[et + 1];
  }
  F0R (i, q) {
    scanf ("%d %d", &interval[i].first, &interval[i].second);
  }
  int tot = 0;
  FOR (i, l, r + 1) {
    F0R (j, N) {
      must[j] = 0;
    }
    F0R (j, q) {
      int st = interval[j].first + i;
      int et = interval[j].second + i;
      ++must[st];
      --must[et + 1];
    }
    tot += solve ();
  }
  printf ("%d\n", tot);
  return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < (int) b; i++)
#define F0R(i, a) FOR (i, 0, a)
#define ROF(i, a, b) for(int i = a; i >= (int) b; i--)
#define R0F(i, a) ROF(i, a, 0)
#define GO(i, a) for (auto i : a)

#define eb emplace_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vpii> vvpii;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef vector<pi64> vpi64;

const int N = 1000 + 500;

int ci, cf;
vpii interval (55);
vi cnt (N + 5);
vi must (N + 5);

bool solve () {
  int cur = 0, chk = 0;
  F0R (i, N) {
    cur += cnt[i];
    chk += must[i];
    if (cur > 0 && chk > 0) return true;
  }
  return false;
}

int main() {
  int p, q, l, r;
  scanf ("%d %d %d %d", &p, &q, &l, &r);
  F0R (i, p) {
    int st, et;
    scanf ("%d %d", &st, &et);
    ++cnt[st];
    --cnt[et + 1];
  }
  F0R (i, q) {
    scanf ("%d %d", &interval[i].first, &interval[i].second);
  }
  int tot = 0;
  FOR (i, l, r + 1) {
    F0R (j, N) {
      must[j] = 0;
    }
    F0R (j, q) {
      int st = interval[j].first + i;
      int et = interval[j].second + i;
      ++must[st];
      --must[et + 1];
    }
    tot += solve ();
  }
  printf ("%d\n", tot);
  return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < (int) b; i++)
#define F0R(i, a) FOR (i, 0, a)
#define ROF(i, a, b) for(int i = a; i >= (int) b; i--)
#define R0F(i, a) ROF(i, a, 0)
#define GO(i, a) for (auto i : a)

#define eb emplace_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vpii> vvpii;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef vector<pi64> vpi64;

const int N = 1000 + 500;

int ci, cf;
vpii interval (55);
vi cnt (10 * N);
vi must (10 * N);

bool solve () {
  int cur = 0, chk = 0;
  F0R (i, N) {
    cur += cnt[i];
    chk += must[i];
    if (cur > 0 && chk > 0) return true;
  }
  return false;
}

int main() {
  int p, q, l, r;
  scanf ("%d %d %d %d", &p, &q, &l, &r);
  F0R (i, p) {
    int st, et;
    scanf ("%d %d", &st, &et);
    ++cnt[st];
    --cnt[et + 1];
  }
  F0R (i, q) {
    scanf ("%d %d", &interval[i].first, &interval[i].second);
  }
  int tot = 0;
  FOR (i, l, r + 1) {
    F0R (j, N) {
      must[j] = 0;
    }
    F0R (j, q) {
      int st = interval[j].first + i;
      int et = interval[j].second + i;
      ++must[st];
      --must[et + 1];
    }
    tot += solve ();
  }
  printf ("%d\n", tot);
  return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a; i < (int) b; i++)
#define F0R(i, a) FOR (i, 0, a)
#define ROF(i, a, b) for(int i = a; i >= (int) b; i--)
#define R0F(i, a) ROF(i, a, 0)
#define GO(i, a) for (auto i : a)

#define eb emplace_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vpii> vvpii;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef vector<pi64> vpi64;

int ci, cf;
vpii interval (55);
vi cnt (1005);
vi must (1005);

bool solve () {
  int cur = 0, chk = 0;
  F0R (i, 1005) {
    cur += cnt[i];
    chk += must[i];
    if (cur > 0 && chk > 0) return true;
  }
  return false;
}

int main() {
  int p, q, l, r;
  scanf ("%d %d %d %d", &p, &q, &l, &r);
  F0R (i, p) {
    int st, et;
    scanf ("%d %d", &st, &et);
    ++cnt[st];
    --cnt[et + 1];
  }
  F0R (i, q) {
    scanf ("%d %d", &interval[i].first, &interval[i].second);
  }
  int tot = 0;
  FOR (i, l, r + 1) {
    F0R (j, 1005) {
      must[j] = 0;
    }
    F0R (j, q) {
      int st = interval[j].first + i;
      int et = interval[j].second + i;
      ++must[st];
      --must[et + 1];
    }
    tot += solve ();
  }
  printf ("%d\n", tot);
  return 0;
}