N = int(raw_input()) S = raw_input() cnt = 0 for i in range(1, N+1): for j in range(i+1, N+1): k = 2*j-i if j < k: if S[i] == 'U' and S[j] == 'M' and S[k] == 'G': cnt += 1 print cnt