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

int n,x,y,ans;
string a;
map <pair <int,int>,int> m;

int main()
{
	cin>>n>>a;

	m[{x,y}]++;
	for(auto c:a)
	{
		if(c=='A') x++;
		else if(c=='T') x--;
		else if(c=='C') y++;
		else y--;

		ans+=m[{x,y}];
		m[{x,y}]++;
	}
	cout<<ans;
}
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include<iomanip>

#define int long long int
using namespace std;
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
int mod(int x)
{
    return (((x)%1000000007)+1000000007)%1000000007;
}

int solve()
{
   int n;
   string s;
   cin>>n>>s;
   int a[n+1],g[n+1],c[n+1],t[n+1];
   for(int i=0;i<n+1;i++)
   {
       a[i]=0;
       c[i]=0;
       g[i]=0;t[i]=0;
   }
   if(s[0]=='A')
   a[1]=1;
   else
   a[1]=0;
   if(s[0]=='G')
   g[1]=1;
   else
   g[1]=0;
   if(s[0]=='C')
   c[1]=1;
   else
   c[1]=0;
   if(s[0]=='T')
   t[1]=1;
   else
   t[1]=0;
   for(int i=2;i<=n;i++)
   {
       if(s[i-1]=='A')
       a[i]=a[i-1]+1;
       else
       a[i]=a[i-1];
   }
   for(int i=2;i<=n;i++)
   {
       if(s[i-1]=='C')
       c[i]=c[i-1]+1;
       else
       c[i]=c[i-1];
   }
   for(int i=2;i<=n;i++)
   {
       if(s[i-1]=='T')
       t[i]=t[i-1]+1;
       else
       t[i]=t[i-1];
   }
   for(int i=2;i<=n;i++)
   {
       if(s[i-1]=='G')
       g[i]=g[i-1]+1;
       else
       g[i]=g[i-1];
   }
   int count=0;
   for(int i=0;i<n;i++)
   {
       for(int j=i;j<n;j++)
       {
           if(a[j+1]-a[i]==t[j+1]-t[i] && c[j+1]-c[i]==g[j+1]-g[i])
           count++;
       }
   }
   cout<<count;
   return 0; 
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
	int t;
	//cin >> t;
	t=1;
	while (t--)
	{
		solve();
	}
	return 0;
}