n = int(input()) s = input() ans = 0 for i in range(n - 2): if s[i] == 'U': try: for k in range(1, (n - i) // 2 + 1): if s[i + k] == 'M' and s[i + k * 2] == 'G': ans += 1 except: err = 1 print(ans)