Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.

Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.

That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.

Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.


-----Input-----

The first line of input contains the integer n (1 ≤ n ≤ 3·10^5) — the number of boxes.

Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1 ≤ x ≤ n) follows, indicating that Daru should add the box with number x to the top of the stack. 

It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed.


-----Output-----

Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.


-----Examples-----
Input
3
add 1
remove
add 2
add 3
remove
remove

Output
1

Input
7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove

Output
2



-----Note-----

In the first sample, Daru should reorder the boxes after adding box 3 to the stack.

In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack.

Source: codeforces | Difficulty: MEDIUM
Tags: Tree algorithms

Data structures

Greedy algorithms


Solutions:
ans = 0
num = 1
stack = []
n = int(input())
for i in range(2 * n):
	s = input()
	if s.find('add') != -1:
		stack.append(int(s.split()[1]))
	else:
		if len(stack) != 0 and stack[-1] != num:
			stack = []
			ans += 1
		if len(stack):
			stack.pop()
		num += 1
print(ans)


n = int(input())
data = []
need = 1
ans = 0
for i in range(2 * n):
	op = input().split()
	if len(op) == 1:
		need += 1
		if data == []:
			continue
		if data[-1] == need - 1:
			del data[-1]
		else:
			data = []
			ans += 1
	else:
		data.append(int(op[1]))
print(ans)


from sys import stdout, stdin
input = stdin.readline
print = stdout.write
n = int(input())
curr = 0
stack = []
ans = 0
t = 1
for i in range(2 * n):
	x = input().split()
	if len(x) == 2:
		stack.append(int(x[1]))
	else:
		if stack:
			if stack[-1] != t:
				ans += 1
				stack = []
			else:
				stack.pop()
		t += 1
print(str(ans))


n = int(input())
cnt = 1
ans = 0
l = list()
for i in range(2 * n):
	s = input()
	if s[0] == 'a':
		l.append(int(s[4:]))
	else:
		if len(l) == 0:
			pass
		elif l[-1] == cnt:
			l.pop()
		else:
			ans += 1
			l = list()
		cnt += 1
print(ans)


import sys
n = int(input())
c = 0
e = 1
s = []
for _ in range(2 * n):
	x = sys.stdin.readline().split()
	if len(x) == 1:
		if s:
			if s[-1] == e:
				s.pop()
			else:
				c += 1
				s = []
		e += 1
	else:
		s += [int(x[1])]
print(c)


n = int(input())
box = []
count = remove_counter = 0
for i in range(2 * n):
	op = input().split()
	if op[0][0] == 'a':
		box.append(int(op[1]))
	if op[0][0] == 'r':
		remove_counter += 1
		if len(box) == 0:
			continue
		if box[-1] == remove_counter:
			del box[-1]
		else:
			box = []
			count += 1
print(count)


n = int(input())
box_stack = []
count = 0
num_removed = 0
for _ in range(2 * n):
	line = input().split(' ')
	if line[0] == 'add':
		box_stack.append(int(line[1]))
	else:
		num_removed += 1
		if len(box_stack) > 0:
			if num_removed != box_stack[-1]:
				count += 1
				box_stack = []
			else:
				box_stack.pop()
print(count)


from heapq import heappush, heappop
import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	reorders = 0
	curr_remove = 1
	ordered_bot = []
	unordered_top = []
	for _ in range(2 * int(input())):
		c = input().split()
		if len(c) == 2:
			a = c[0]
			b = int(c[1])
		else:
			a = c[0]
		if a == 'add':
			unordered_top.append(b)
		else:
			if len(unordered_top):
				if curr_remove == unordered_top[-1]:
					unordered_top.pop()
				else:
					reorders += 1
					for i in unordered_top:
						heappush(ordered_bot, i)
					unordered_top = []
					heappop(ordered_bot)
			else:
				heappop(ordered_bot)
			curr_remove += 1
	print(reorders)
prog()


n = int(input())
pointer = 1
series = [0] * n
stack = []
reset = 0
for i in range(1, n * 2 + 1):
	inp = input().split(' ')
	if inp[0] == 'add':
		number = int(inp[1])
		stack.append(number)
	if inp[0] == 'remove':
		if len(stack) == 0:
			pointer += 1
		elif stack[-1] == pointer:
			del stack[-1]
			pointer += 1
		else:
			reset += 1
			stack = []
			pointer += 1
print(reset)


t = int(input())
ans = 0
curr = 1
lst = []
for i in range(2 * t):
	s = input().split()
	if s[0] == 'add':
		n = int(s[1])
		lst.append(n)
	else:
		if lst != []:
			if lst[-1] == curr:
				lst.pop()
			else:
				lst = []
				ans += 1
		curr += 1
print(ans)


import sys
import queue

def main(lines):
	n = int(lines[0])
	result = 0
	last_rebuild = -1
	required_box = 0
	boxes = []
	add_time = [0] * n
	is_deleted = [False] * n
	for i in range(2 * n):
		query = lines[i + 1].split()
		if query[0] == 'add':
			id = int(query[1]) - 1
			boxes.append((i, id))
		else:
			last_box = boxes[-1]
			while is_deleted[last_box[1]]:
				boxes.pop()
				last_box = boxes[-1]
			if last_box[1] != required_box and last_rebuild < last_box[0]:
				result += 1
				last_rebuild = i
			is_deleted[required_box] = True
			required_box += 1
	print(result)
main(sys.stdin.readlines())


n = int(input())
c = 1
sorts = 0
boxes = []
for _ in range(2 * n):
	s = input()
	if s[0] == 'a':
		boxes.append(int(s[4:]))
	else:
		if boxes:
			if boxes[-1] != c:
				boxes = []
				sorts += 1
			else:
				boxes.pop()
		c += 1
print(sorts)


from collections import deque
import sys
n = int(input())
q = deque([])
toBe = 1
ans = 0
flag = 0
inp = [0] * (n * 2)
for i in range(2 * n):
	inp[i] = sys.stdin.readline().strip()
for i in range(2 * n):
	s = inp[i].split()
	if len(s) == 2:
		k = int(s[1])
		q.append(k)
		if len(q) == 1:
			continue
		if k != q[-2] - 1:
			flag = 0
	else:
		if q and toBe != q[-1]:
			flag = 1
			ans += 1
			q = []
		elif flag == 1:
			q = []
		else:
			q.pop()
		toBe += 1
sys.stdout.write(str(ans) + '\n')


import sys

def main():
	n = int(input())
	n = n * 2
	u = 0
	res = 0
	x = []
	for i in range(n):
		s = sys.stdin.readline()
		if s[0] == 'r':
			u += 1
			if len(x) == 0:
				continue
			if x[-1] == u:
				x.pop()
			else:
				x = []
				res += 1
		else:
			(a, b) = s.split()
			x.append(int(b))
	print(res)
main()


n = int(input())
indexToDel = 1
stack = []
count = 0
for i in range(n * 2):
	line = input().split()
	if line[0] == 'remove':
		if len(stack) != 0:
			if stack[-1] == indexToDel:
				stack.pop()
			else:
				count += 1
				stack = []
		indexToDel += 1
	else:
		stack.append(int(line[1]))
print(count)


n = int(input())
ptr1 = 0
ptr2 = 0
counter = 1
ans = 0
a = [0] * (n + 1)
for i in range(2 * n):
	s = input().split()
	if len(s) == 2:
		ptr1 += 1
		a[ptr1] = int(s[1])
	else:
		if ptr1 != ptr2:
			if a[ptr1] != counter:
				ptr2 = ptr1
				ans += 1
				ptr1 -= 1
				ptr2 -= 1
			else:
				ptr1 -= 1
		else:
			ptr1 -= 1
			ptr2 -= 1
		counter += 1
print(ans)


n = int(input())
stck = []
st = set()
cur_remove = 1
ans = 0
for q in range(n * 2):
	s = input().split()
	c = s[0]
	if c == 'add':
		val = int(s[1])
		stck.append(val)
	else:
		if len(stck) > 0 and cur_remove == stck[len(stck) - 1] or (len(stck) == 0 and cur_remove in st):
			if len(stck) > 0 and cur_remove == stck[len(stck) - 1]:
				stck.pop()
			cur_remove += 1
			continue
		ans += 1
		for j in stck:
			st.add(j)
		stck.clear()
		cur_remove += 1
print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
l = list()
shuffle = 0
res = 0
remove = 1
count = 0
for i in range(2 * n):
	inp = list(input().split())
	if inp[0] == 'add':
		l.append(int(inp[1]))
		count += 1
	else:
		if len(l) == 0:
			g = 0
		else:
			g = l.pop()
			if g == remove:
				g = 0
			else:
				l = list()
				res += 1
		remove += 1
print(res)


from sys import stdin, stdout

def main():
	n = int(stdin.readline().strip())
	a = [0 for i in range(n + 1)]
	top = -1
	reordered = 0
	toRemove = 1
	for x in range(n + n):
		cmmd = stdin.readline().strip()
		if cmmd[0] == 'a':
			k = int(cmmd.split(' ')[1])
			top += 1
			a[top] = k
		else:
			if toRemove == a[top] or top == -1:
				top = max(top - 1, -1)
			else:
				top = -1
				reordered += 1
			toRemove += 1
	stdout.write(str(reordered) + '\n')
main()


n = int(input())
c = 1
l = []
t = 0
for _ in range(2 * n):
	i = input().split()
	if i[0] == 'add':
		l.append(int(i[1]))
	else:
		if l:
			if l[-1] == c:
				l.pop()
			else:
				t += 1
				l = []
		c += 1
print(t)


stack = []
next_pop_num = 1
r = 0
for _ in range(2 * int(input())):
	command = input()
	if command.startswith('add'):
		n = int(command[4:])
		stack.append(n)
	else:
		if stack and stack[-1] != next_pop_num:
			r += 1
			stack = []
		if stack:
			stack.pop(-1)
		next_pop_num += 1
print(r)


n = int(input())
list = []
ans = 0
ptr = 1
for i in range(2 * n):
	s = input()
	if s[0] == 'a':
		list.append(int(s[4:]))
	elif len(list) == 0:
		ptr += 1
	elif list[-1] == ptr:
		list.pop()
		ptr += 1
	else:
		list = []
		ans += 1
		ptr += 1
print(ans)


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
int1 = lambda x: int(x) - 1
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))
ilelec = lambda : map(int1, input().split())
alelec = lambda : list(map(int1, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
N = int(input())
A = deque()
Ans = 0
top = 0
for i in range(2 * N):
	s = input()
	if s == 'remove':
		top += 1
		if len(A) == 0:
			continue
		elif top == A[-1]:
			A.pop()
		else:
			Ans += 1
			A = deque()
	else:
		(_, n) = s.split(' ')
		n = int(n)
		A.append(n)
print(Ans)


n = int(input())
mas = []
p = 0
r = 0
for i in range(2 * n):
	s = input().split()
	if s[0] == 'add':
		mas.append(int(s[1]))
	else:
		r += 1
		if len(mas) > 0 and r == mas[-1]:
			del mas[-1]
		elif len(mas) > 0:
			p += 1
			mas = []
print(p)


from heapq import heappush, heappop, nsmallest
n = int(input())
stack_head = -1
rems = 0
ans = 0
nums = []
stack = []
for i in range(2 * n):
	command = input().split()
	if command[0] == 'add':
		box = int(command[1])
		stack.append(box)
	else:
		rems += 1
		if len(stack) == 0:
			continue
		elif stack[-1] == rems:
			stack.pop()
		else:
			ans += 1
			stack.clear()
print(ans)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
st = [(0, True)]
now = 0
ans = 0
for i in range(2 * n):
	com = input()
	try:
		(com, x) = com.split()
		x = int(x)
	except:
		pass
	if com == 'add':
		st.append((x, False))
	else:
		now += 1
		(last, change) = st.pop()
		if last == now:
			if change:
				st[-1] = (now + 1, change)
		else:
			ans += not change
			st[-1] = (now + 1, True)
print(ans)


def rever(l):
	l1 = []
	for i in range(len(l)):
		l1.append(l[len(l) - 1 - i])
	return l1
n = int(input())
l = []
ans = 0
r = 1
ind = 0
ad = 0
for i in range(2 * n):
	s = str(input())
	if s[0] == 'a':
		ad += 1
		l.append(int(s[4:]))
	elif s[0] == 'r':
		if len(l) > 0:
			if l[-1] != r:
				ans += 1
				l = []
			else:
				l.pop()
		r += 1
print(ans)


n = int(input())
res = 0
next_to_erase = 1
remove = 1
stack = []
i = 0
line = []
while i < 2 * n:
	line = input()
	i += 1
	if line[0] == 'a':
		stack.append(int(line.split()[1]))
	else:
		if not stack:
			next_to_erase += 1
		elif remove == stack[-1]:
			stack.pop()
		else:
			res += 1
			stack = []
			next_to_erase += 1
		remove += 1
print(res)


import sys
S = []
A = set()
cnt = 0
r = 0
for i in range(2 * int(sys.stdin.readline().strip())):
	cmd = sys.stdin.readline().strip().split()
	if cmd[0] == 'add':
		S.append(int(cmd[1]))
	else:
		r += 1
		if S == []:
			if r in A:
				continue
		elif S[-1] == r:
			S.pop()
			continue
		else:
			cnt += 1
			A.union(S)
			S = []
print(cnt)


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
n = int(input())
st = []
ans = 0
curr = 1
for _ in range(2 * n):
	op = list(input().strip().split())
	if op[0] == 'add':
		st.append(int(op[1]))
	else:
		if st:
			if st[-1] == curr:
				st.pop()
			else:
				ans += 1
				st = []
		curr += 1
print(ans)


from heapq import heappush, heappop
n = int(input())
heap = []
ans = 0
last_added = []
for i in range(2 * n):
	c = input()
	if c == 'remove':
		out = heappop(heap)
		if len(last_added):
			if out != last_added[-1]:
				ans += 1
				last_added = []
			else:
				last_added.pop()
	else:
		b = int(c.split()[1])
		last_added.append(b)
		heappush(heap, b)
print(ans)


n = int(input())
st = []
(ans, now) = (0, 1)
for i in range(2 * n):
	s = input()
	if s[0] == 'a':
		st.append(int(s[4:]))
	elif len(st) == 0:
		now += 1
	elif st[-1] == now:
		now += 1
		st.pop()
	else:
		st = []
		ans += 1
		now += 1
print(ans)


from collections import deque
n = int(input())
stack = deque()
(start, end) = (1, 1)
items_added = 0
reorder = 0
for i in range(2 * n):
	command = input()
	if command[0] == 'a':
		items_added += 1
		stack.append(int(command.replace('add ', '')))
	else:
		if len(stack):
			if stack[-1] == start:
				stack.pop()
			else:
				reorder += 1
				end = items_added
				stack = deque()
		start += 1
print(reorder)


def remove():
	global c, tot, s
	c += 1
	if not s:
		return
	elif s.pop() == c - 1:
		return
	(tot, s) = (tot + 1, [])
(s, tot, c) = ([], 0, 1)
for _ in range(int(input()) * 2):
	cmd = list(input().split())
	if cmd[0] == 'add':
		s.append(int(cmd[1]))
	else:
		remove()
print(tot)


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
n = int(input())
cnt = 0
curr = 1
str = []
stack = []
for i in range(2 * n):
	str = list(input().split())
	cmd = str[0]
	if len(str) == 2:
		num = int(str[1])
	if cmd == 'add':
		stack.append(num)
	else:
		if len(stack) != 0:
			if stack[-1] == curr:
				stack.pop()
			else:
				stack = []
				cnt += 1
		curr += 1
print(cnt)


n_boxes = int(input())
stack = []
b = 1
count = 0
for i in range(2 * n_boxes):
	input_string = input().split(' ')
	if len(input_string) == 2:
		x = int(input_string[1])
		stack.append(x)
	else:
		if len(stack) != 0:
			if stack[-1] == b:
				stack.pop()
			else:
				count += 1
				stack = []
		b += 1
print(count)


n = 2 * int(input())
st = []
cur = 1
ans = 0
for _ in range(n):
	op = input().split()
	if op[0] == 'add':
		val = int(op[1])
		st.append(val)
	else:
		if st:
			if st[-1] == cur:
				st.pop()
			else:
				ans += 1
				st = []
		cur += 1
print(ans)


n = int(input())
mas = []
current = 1
count = 0
for i in range(2 * n):
	instruct = input().split()
	if instruct[0] == 'add':
		mas.append(int(instruct[1]))
	else:
		if len(mas) > 0:
			if mas[-1] == current:
				mas.pop()
			else:
				mas = []
				count += 1
		current += 1
print(count)


t = int(input())
l = []
c = 0
ln = 0
for i in range(0, 2 * t):
	q = input().split()
	if q[0] == 'add':
		a = q[1]
		a = int(a)
		l.append(a)
	else:
		ln = ln + 1
		if len(l):
			if ln != l[-1]:
				c = c + 1
				l = []
			else:
				l.pop()
print(c)


n = int(input())
s = []
j = 0
a = 0
for i in range(2 * n):
	q = input()
	if q == 'remove':
		if len(s) > 0:
			x = s.pop()
			if x != j + 1:
				a += 1
				s = []
		j += 1
	else:
		s.append(int(q[3:]))
print(a)


import math as mt
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
d = defaultdict(int)
n = I()
l = []
k = 1
c = 0
j = 1
pointer = -1
for i in range(2 * n):
	s = input().split()
	if s[0] == 'add':
		l.append(int(s[1]))
	if s[0] == 'remove':
		if len(l) > 0:
			if l[-1] == k:
				l.pop()
			else:
				c += 1
				l = []
		k += 1
print(c)


def solve():
	n = get(int)
	commands = [get([str, int]) for i in range(2 * n)]
	stack = []
	stacklen = 0
	toremove = 1
	shuffles = 0
	for cm in commands:
		if len(cm) == 2:
			val = cm[1]
			stack.append(val)
			stacklen += 1
		else:
			if stacklen > 0:
				if stack[-1] == toremove:
					stack.pop()
					stacklen -= 1
				else:
					stack = []
					stacklen = 0
					shuffles += 1
			toremove += 1
	return shuffles
_testcases = '\n3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove\n\n1\n\n\n7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove\n\n2\n\n'.strip()
import re
import sys
import math
import heapq
from heapq import heapify, heappop, heappush
import bisect
import string
from bisect import bisect_left, bisect_right
import operator
from operator import itemgetter, attrgetter
import itertools
import collections
inf = float('inf')
sys.setrecursionlimit(10000)

def tree():
	return collections.defaultdict(tree)

def cache(func):
	cache_dict = {}

	def _cached_func(*args, _get_cache=False):
		if _get_cache:
			return cache_dict
		if args in cache_dict:
			return cache_dict[args]
		cache_dict[args] = func(*args)
		return cache_dict[args]
	return _cached_func

def equal(x, y, epsilon=1e-06):
	if -epsilon <= x - y <= epsilon:
		return True
	if -epsilon <= x <= epsilon or -epsilon <= y <= epsilon:
		return False
	return -epsilon <= (x - y) / x <= epsilon or -epsilon <= (x - y) / y <= epsilon

def get(_type):
	if type(_type) == list:
		if len(_type) == 1:
			_type = _type[0]
			return list(map(_type, input().strip().split()))
		else:
			return [_type[i](inp) for (i, inp) in enumerate(input().strip().split())]
	else:
		return _type(input().strip())
(_p, print) = (print, lambda *a, **b: None)
_p(solve())


import heapq
qty = 0
stack = []
n = 2 * int(input())
cur = 1
cmds = {'add': 1, 'remove': 2}
for i in range(n):
	cmd_list = input().split()
	cmd = cmds.get(cmd_list[0])
	if cmd == 1:
		heapq.heappush(stack, (-i, cmd_list[1]))
	else:
		if stack:
			if int(stack[0][1]) == cur:
				heapq.heappop(stack)
			else:
				qty += 1
				while stack:
					heapq.heappop(stack)
		cur += 1
print(qty)


n = int(input())
data = []
need = 1
answer = 0
i = 0
while i != n:
	s = input()
	if s[0:3] == 'add':
		data.append(int(s[4:]))
	else:
		if len(data) == 0:
			need += 1
		elif data[-1] == need:
			need += 1
			data.pop()
		else:
			answer += 1
			data = []
			need += 1
		i += 1
print(answer)


from sys import stdout, stdin
input = stdin.readline
n = int(input())
(stk, res, t) = ([], 0, 1)
for i in range(2 * n):
	x = input().split()
	if len(x) == 2:
		stk.append(int(x[1]))
		continue
	if stk:
		if stk[-1] != t:
			res += 1
			stk = []
		else:
			stk.pop()
	t += 1
print(str(res))


import sys

def main(input):
	n = int(next(input))
	result = 0
	last_rebuild = -1
	required_box = 0
	boxes = []
	for i in range(2 * n):
		query = next(input).split()
		if query[0] == 'add':
			id = int(query[1]) - 1
			boxes.append((i, id))
		else:
			last_box = boxes[-1]
			while last_box[1] < required_box:
				boxes.pop()
				last_box = boxes[-1]
			if last_box[1] != required_box and last_rebuild < last_box[0]:
				result += 1
				last_rebuild = i
			required_box += 1
	print(result)
main(iter(sys.stdin.readlines()))


from sys import *
input = stdin.readline
n = int(input())
nxt = 1
curr = []
ordered = set()
cnt = 0
for i in range(2 * n):
	cmd = input()
	if cmd[:3] == 'add':
		(_, val) = cmd.split()
		curr.append(int(val))
	else:
		if curr:
			if curr[-1] == nxt:
				curr.pop()
			else:
				cnt += 1
				curr = []
		nxt += 1
print(cnt)


from sys import stdin, stdout
n = int(stdin.readline())
used = [0 for i in range(n + 1)]
delete = set()
stack = []
ans = 0
cnt = set()
number = 1
label = 0
for i in range(2 * n):
	s = stdin.readline().strip().split()
	while stack and stack[-1] in delete:
		cnt.discard(stack[-1])
		stack.pop()
	if s[0] == 'add':
		stack.append(int(s[1]))
		if label:
			cnt.add(int(s[1]))
	else:
		if label and (not len(cnt)):
			delete.add(number)
		elif stack[-1] == number:
			cnt.discard(stack[-1])
			stack.pop()
		else:
			ans += 1
			cnt = set()
			delete.add(number)
			label = 1
		number += 1
stdout.write(str(ans))


n = int(input())
st = []
ans = 0
c = 0
for _ in range(2 * n):
	s = input()
	if s == 'remove':
		if len(st) == 0 or c + 1 == st[-1]:
			if len(st) != 0:
				st.pop(-1)
			c += 1
		else:
			ans += 1
			c += 1
			st = []
	else:
		st.append(int(s[4:]))
print(ans)


def main():
	N = int(input())
	Q = 2 * N
	stack = []
	curr = 1
	ans = 0
	for _ in range(Q):
		A = input().strip().split(' ')
		if len(A) > 1:
			num = int(A[1])
			stack += [num]
		else:
			if stack != []:
				if stack[-1] == curr:
					stack.pop()
				else:
					ans += 1
					stack = []
			curr += 1
	print(ans)
main()


n = int(input())
ans = 0
p = 0
l = []
k = 0
for x in range(n * 2):
	com = input().split()
	if com[0] == 'add':
		v = int(com[1])
		if p == 0:
			l.append(v)
		else:
			p -= 1
			l[-1 - p] = v
	else:
		k += 1
		try:
			if len(l) > 0:
				if l[-1 - p] != k:
					p = 0
					ans += 1
					l = []
				else:
					p += 1
		except Exception as e:
			l = []
			p = 0
print(ans)


n = int(input())
(arr, curr, res, i) = ([], 1, 0, 0)
while i < n:
	s = input()
	if s[0] == 'a':
		arr.append(int(s[4:]))
		continue
	if len(arr) == 0:
		curr += 1
	elif arr[-1] == curr:
		curr += 1
		arr.pop()
	else:
		res += 1
		arr = []
		curr += 1
	i += 1
print(res)


from collections import deque
queue = deque()
ans = 0
cou = 1
n = int(input())
for j in range(2 * n):
	a = input().split()
	if a[0] == 'add':
		queue.append(int(a[1]))
	else:
		if queue:
			if cou == queue[-1]:
				queue.pop()
			else:
				ans += 1
				queue = deque()
		cou += 1
print(ans)


from collections import deque
n = int(input())
s = 0
c = 1
q = deque([])
l = False
for i in range(2 * n):
	t = input()
	if t[0] == 'a':
		m = int(t[4:])
		q.append(m)
		l = False
	else:
		if len(q) > 0:
			f = q.pop()
			if l == False and f != c:
				s += 1
				l = True
				q = []
		c += 1
print(s)