# Enter your code here. Read input from STDIN. Print output to STDOUT n = gets.strip.to_i seq = gets.strip.split('') def counting_valleys(seq) current_level = 0 num_of_valleys = 0 in_valley = false seq.each do |s| current_level += s == 'U' ? 1 : -1 if current_level < 0 && !in_valley in_valley = true end if current_level == 0 && in_valley in_valley = false num_of_valleys += 1 end end num_of_valleys end puts counting_valleys(seq)