n=int(input()) s=input() ans=0 for i in range(n-2): for j in range(i+1,n-1): if (i+j)%2==1: continue if s[i]=="U" and s[(i+j)//2]=="M" and s[j]=="G": ans+=1 print(ans)