function solution(order) {
var answer = 0;
let stack=[];
let idx=1;
for (let i=0;i<order.length;i++){
let isSide=true;
while(isSide){
if (order[i]===stack.at(-1)){
isSide=false;
stack.pop();
answer+=1;
}else{
if (order[i]<stack.at(-1)){
return answer;
}
stack.push(idx++);
}
}
}
return answer;
}