编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
n = int(input())
operations = [input().strip() for _ in range(2 * n)]

current = 1
adjust_count = 0
queue = []

for op in operations:
    if op.startswith('head add'):
        x = int(op.split()[-1])
        queue.insert(0, x)
    elif op.startswith('tail add'):
        x = int(op.split()[-1])
        queue.append(x)
    elif op == 'remove':
        if queue and queue[0] == current:
            queue.pop(0)
            current += 1
        else:
            # 需要调整顺序
            adjust_count += 1
            queue.sort()
            # 确保当前要删除的元素在队首
            while queue and queue[0] < current:
                queue.pop(0)
            if queue and queue[0] == current:
                queue.pop(0)
                current += 1

print(adjust_count)