function processData(input) { var seaLevel = valleyCount = 0; var startValley = false; var steps = input.split('\n')[1].split(''); steps.forEach(function(step) { seaLevel += (step === 'U' ? 1 : -1); if(seaLevel == -1) { startValley = true; } if(seaLevel == 0 && startValley) { startValley = false; valleyCount++; } }); console.log(valleyCount); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });