import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np N = int(readline()) S = np.frombuffer(read(),'S1')[:N] M = (S == b'M') U = (S == b'U') G = (S == b'G') answer = 0 for i in np.where(M)[0]: L = min(i, N-1-i) left = U[:i][::-1][:L] right = G[i+1:][:L] answer += np.count_nonzero(left&right) print(answer)