N = int(input()) S = list(input()) c = S.count("U") count = 0 i = 0 while c > 0: if S[i] == "U": for j in range(int((N-i-1)/2)): if S[i+j+1] == "M" and S[i+2*(j+1)] == "G": count += 1 c -= 1 i += 1 print(count)