object Solution { def main(args: Array[String]) { def stepToLevel(step: Char, lastLevel: Int): Int = step match { case 'U' => lastLevel + 1 case 'D' => lastLevel - 1 } def stepsToLevel(steps: List[Char], acc: List[Int]): List[Int] = steps match { case List() => acc case x :: xs => stepsToLevel(xs, stepToLevel(x, acc.head) :: acc) } def countValeys(levels: List[Int], acc: Int): Int = levels match { case x :: y :: xs => countValeys(levels.tail, if (x == 0 && y < 0) acc + 1 else acc) case _ => acc } val sc = new java.util.Scanner(System.in) val steps = sc.nextInt() sc.nextLine() val line = sc.nextLine() System.out.println(countValeys(stepsToLevel(line.toList, List(0)), 0)) } }