#include "bits/stdc++.h"
using namespace std;
#define dbg(var) cout<<#var<<"="<<var<<" "
#define nl cout<<"\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fa(v) for(auto &i:v)
#define all(v) v.begin(),v.end()
#define sz(v) (int)(v.size())
//#define int long long
void solve(){
  int n; cin >> n;
  string s; cin >> s;
  s = "#" + s;
  int dp[n+4][30];memset(dp,0,sizeof dp);
  rep(i,1,n) dp[i][s[i] - 'A']++;
  fr(j,30)
  rep(i,1,n){
  	dp[i][j] += dp[i-1][j];
  }
  int ans = 0;
  auto gfr = [&](int l,int r,char c){
  	return dp[r][c-'A'] - dp[l-1][c-'A'];
  };
  rep(i,1,n){
  	for(int j = i+1; j<=n; j+=2){
  		if(gfr(i,j,'A') == gfr(i,j,'T') and gfr(i,j,'C') == gfr(i,j,'G'))
  			ans++;
  	}
  }
  cout << ans << "\n";
}
int32_t main()
{
   ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
   // int tst; cin >> tst; while(tst--)
   {
   	solve();
   }
}
//Solution By SlavicG
#include "bits/stdc++.h"

using namespace std;

#define              ll              long long

#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()

#define            pb                push_back
#define          sz(a)               (int)a.size()
#define         fastio               ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll pw(ll a,ll b,ll m){a%=m;ll r=1;while(b>0){if(b&1)r=(r*a)%m;a=(a*a)%m;b/=2;}return r;}
ll gcd(ll a, ll b) {if(!b)return a;return gcd(b,a % b);}
ll lcm(ll a,ll b){return(a*b)/gcd(a,b);}
ll fact(ll n, ll mod){ll ret=1;for(ll i=1;i<=n;i++)ret=(ret*i)%mod;return ret;}
bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(ll i=5;i*i <= n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
const int N  = 2000;
char c[N][N];
int dist[N][N];
bool vis[N][N];
vector<pair<pair<int,int>,int>> adj[N][N];
void solve() {
 	int n,m;
 	cin >> n >> m;
 	char c[n][m];
 	int startI = -1, startJ = -1;
 	int endI = -1, endJ = -1;
 	vector<pair<pair<int,int>,int>> v[26];
 	for(int i =0;i < n;i++)
 	{
 		string s;
 		cin >> s;
 		for(int j= 0;j < m;j++)
 		{
 			c[i][j] = s[j];
 			if(c[i][j] >= 'a' && c[i][j] <= 'z')
 			{
 				v[c[i][j] - 'a'].pb({{i,j},1});
 			}
 			if(c[i][j] == 'S'){
 				startI = i, startJ = j;
 			}
 			if(c[i][j] == 'G'){
 				endI = i, endJ = j;
 			}
 		}
 	}
 
 	for(int i =0;i < n;i++)
 	{
 		for(int j = 0;j < m;j++)
 		{
 			if(c[i][j] == '#')continue;
 				if(j + 1 < m)
 				{
 					if(c[i][j+1] != '#')
 					{
 						adj[i][j].pb({{i,j + 1},1});
 						adj[i][j+1].pb({{i,j}, 1});
 					}
 				}
 				if(i +1 < n)
 				{
 					if(c[i+1][j] != '#')
 					{
 						adj[i][j].pb({{i+1,j},1});
 						adj[i+1][j].pb({{i,j},1});
 					}
 				}
 		}
 	}
 	bool taken[26];
 	for(int i= 0;i < 26;i++)
 		taken[i] = false;
 	priority_queue<pair<int,pair<int,int>>> q;
 	q.push({0,{startI,startJ}});
 	for(int i =0;i < n;i++)
 		for(int j = 0; j < m;j++){
 			dist[i][j] = 1e8;
 		}
 	dist[startI][startJ] = 0;
 	while(!q.empty())
	{
		int a = q.top().second.first, b = q.top().second.second;
		q.pop();
		if(vis[a][b])continue;
		vis[a][b] = true;
		if(c[a][b] >= 'a' && c[a][b] <='z' && !taken[c[a][b] - 'a'])
		{
			taken[c[a][b] - 'a'] = true;
			for(auto i : v[c[a][b]-'a'])
			{
				int x = i.first.first;
				int y = i.first.second;
				int weight = i.second;
				if(dist[x][y] > dist[a][b] + weight){
					dist[x][y] = dist[a][b] + weight;
					q.push({-dist[x][y], {x,y}});
				}
			}
		}
		for(auto i : adj[a][b])
		{
			int x = i.first.first;
			int y = i.first.second;
			int weight = i.second;
			if(dist[x][y] > dist[a][b] + weight){
				dist[x][y] = dist[a][b] + weight;
				q.push({-dist[x][y], {x,y}});
			}
		}
	}
	if(dist[endI][endJ] == 1e8){
		cout << -1;
		return;
	}
 	cout << dist[endI][endJ];
}

int32_t main()
{
    fastio;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}