#include <bits/stdc++.h>
using namespace std;
    
#define M 998244353
#define inf 1e8
#define pb push_back
#define ff first
#define ss second
#define int long long int
#define ld long double
#define pi pair<li, li>
#define ins insert
#define vc vector
#define umap unordered_map
#define uset unordered_set
#define all(x) x.begin(), x.end()
#define fr(i, a, n) for (int i = a; i < n; i++)
#define frr(i, a, n) for (int i = n - 1; i >= a; i--)
#define file_in freopen("a.txt", "r", stdin);
const int mxn=1e6;
    
void tc() {
    int n; cin>>n;
    string s; cin>>s;
    map<int,int> m;
    m[0] = 1; 
    int csum = 0 , ans=0;
    for(int i=0; i<n; i++) {
        if(s[i] == 'A')
            csum -= inf;
        else if(s[i] == 'T')
            csum += inf;
        else if(s[i] == 'G')
            csum += 1;
        else 
            csum -= 1;
        
        if(m.count(csum))
            ans += m[csum];
        m[csum]+=1;
    }
    cout<<ans<<"\n";
}
int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; t=1;
    // cin>>t;
    while(t--)  tc();
}
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cstdio>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 5010;
int n,f[N][N][4];
string s;
int main(){
//	freopen("1.txt","r",stdin);
	cin>>n>>s;
	int sum=0;
	for(int i=0;i<n;i++){
		int a=0,t=0,c=0,g=0;
		for(int j=i;j<n;j++){
			if(s[j]=='A') a++;
			else if(s[j]=='T') t++;
			else if(s[j]=='C') c++;
			else g++;
			if(a==t&&c==g) sum++;
		}
	}
	cout<<sum<<endl;
	return 0;
}


