function processData(input) { //Enter your code here var [n, steps] = input.split('\n'); n = parseInt(n); var level = 0; var nextLevel; var valleys = 0; for (let i = 0; i < n; i++) { nextLevel = steps[i] === 'U' ? level+1 : level-1; if (level == 0 && nextLevel == -1) { // valley start valleys++; } level = nextLevel; } console.log(valleys); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });