function processData(input) { //Enter your code here var a = input.split("\n"); var b = a[0]; var c = a[1].split(""); var SEALEVEL = 0; var current = 0; var last = "above"; var valley = 0; for(var i= 0; i < b; i++){ if(c[i] === "U"){ current += 1; if(current >= 0){ last = "above" } } else { current -= 1 if(current < 0 && last !== "below"){ valley ++; last = "below" } } } console.log(valley); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });