from collections import deque N = int(input()) S = input() U = deque([]) M = deque([]) G = deque([]) for i,s in enumerate(S): if s == "U": U.append(i) elif s == "M": M.append(i) else: G.append(i) U,M,G = set(U),set(M),set(G) ans = 0 flag = True try: minU = min(U) maxM = max(M) maxG = max(G) except ValueError: flag = False print(0) step = 1 if flag: while True: if minU+step > maxM or minU+2*step > maxG: break for n in U: if n+step in M and n+2*step in G: ans += 1 step += 1 print(ans)