from collections import deque
def solution(order):
answer = 0 ; ans = []
ls = [i+1 for i in range(len(order))]
subcontainer = deque() ; container = deque(ls)
for i in range(len(order)):
if order[i] in subcontainer and order[i]==subcontainer[0]:
answer += 1
ans.append(subcontainer.popleft())
elif order[i] in subcontainer and order[i]!=subcontainer[0]:
break
else:
while True:
k = container.popleft()
if order[i] == k:
answer += 1
ans.append(k)
break
else:
subcontainer.appendleft(k)
return answer