function processData(input) { //Enter your code here var val_count = 0; var el = 0; var temp = input.split("\n"); temp[1].split('').forEach(function(d) { el = d === "D" ? el - 1 : el + 1; if(el === 0 && d === 'U'){ val_count += 1; } }); console.log(val_count); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });