n = gets.to_i line = gets.chomp.split("") count = 0 line.each_with_index do |v, i| if v == 'M' head, tail = line[0, i].reverse, line[i + 1, n] size = [head.count, tail.count].min size.times do |j| count += 1 if head[j] == 'U' && tail[j] == 'G' end end end puts count