n = gets.to_i s = gets.chomp ans = 0 (0...n-2).each do |i| next unless s[i] == 'U' ((i+1)...n-1).each do |j| next unless s[j] == 'M' if s[j+j-i] == 'G' ans += 1 end end end puts ans