Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types.

  speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type);  overtake is allowed: this sign means that after some car meets it, it can overtake any other car;  no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign);  no overtake allowed: some car can't overtake any other car after this sign. 

Polycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it's kind (speed limit/overtake). It is possible that two or more "no overtake allowed" signs go one after another with zero "overtake is allowed" signs between them. It works with "no speed limit" and "overtake is allowed" signs as well.

In the beginning of the ride overtake is allowed and there is no speed limit.

You are given the sequence of events in chronological order — events which happened to Polycarp during the ride. There are events of following types:

  Polycarp changes the speed of his car to specified (this event comes with a positive integer number);  Polycarp's car overtakes the other car;  Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer);  Polycarp's car goes past the "overtake is allowed" sign;  Polycarp's car goes past the "no speed limit";  Polycarp's car goes past the "no overtake allowed"; 

It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).

After the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn't notice some of the signs. What is the minimal number of signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view?


-----Input-----

The first line contains one integer number n (1 ≤ n ≤ 2·10^5) — number of events.

Each of the next n lines starts with integer t (1 ≤ t ≤ 6) — the type of the event.

An integer s (1 ≤ s ≤ 300) follows in the query of the first and the third type (if it is the query of first type, then it's new speed of Polycarp's car, if it is the query of third type, then it's new speed limit).

It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).


-----Output-----

Print the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view.


-----Examples-----
Input
11
1 100
3 70
4
2
3 120
5
3 120
6
1 150
4
3 300

Output
2

Input
5
1 100
3 200
2
4
5

Output
0

Input
7
1 20
2
6
4
6
6
2

Output
2



-----Note-----

In the first example Polycarp should say he didn't notice the "speed limit" sign with the limit of 70 and the second "speed limit" sign with the limit of 120.

In the second example Polycarp didn't make any rule violation.

In the third example Polycarp should say he didn't notice both "no overtake allowed" that came after "overtake is allowed" sign.

Difficulty: interview

Starter code:

Solutions:
speeds = [1000000]
overtakes = [True]
count = 0
speed = 0
n = int(input())
for e in range(n):
  inp = list(map(int, input().split()))
  # print(inp)
  if inp[0] == 4:
    overtakes.append(True)
  elif inp[0] == 6:
    overtakes.append(False)
  elif inp[0] == 5:
    speeds.append(1000000)
  elif inp[0] == 3:
    speeds.append(inp[1])
    while speed > speeds[-1]:
      count += 1
      speeds.pop()
      
  elif inp[0] == 2:
    while not overtakes[-1]:
      count += 1
      overtakes.pop()
  else:
    while inp[1] > speeds[-1]:
      count += 1
      speeds.pop()
    speed = inp[1]
  
print(count)
"""
Polycarp changes the speed of his car to specified (this event comes with a positive integer number);
Polycarp's car overtakes the other car;
Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer);
Polycarp's car goes past the "overtake is allowed" sign;
Polycarp's car goes past the "no speed limit";
Polycarp's car goes past the "no overtake allowed";
"""

# D. Driving Test

n = int(input())

ps = None
seen_sl = [float('inf')]
seen_ot = [True]

answer = 0
for i in range(n):
	line = list(map(int, input().split()))
	t = line[0]
	if t == 1:
		ps = line[1]
		while ps > seen_sl[-1]:
			answer += 1
			seen_sl.pop(-1)
	elif t == 2:
		while not seen_ot[-1]:
			answer += 1
			seen_ot.pop(-1)
	elif t == 3:
		seen_sl.append(line[1])
		while ps > seen_sl[-1]:
			answer += 1
			seen_sl.pop(-1)
	elif t == 4:
		seen_ot.append(True)
	elif t == 5:
		seen_sl.append(float('inf'))
	elif t == 6:
		seen_ot.append(False)

print(answer)


n = int(input())

events = []
for i in range(n):
    events.append(list([int(s) for s in input().split()]))

max_speeds = []
overtaking_forbidden = 0

current_speed = 0

ignores = 0

for event in events:
    if event[0] == 1:
        current_speed = event[1]

        while max_speeds and current_speed > max_speeds[-1]:
            max_speeds.pop()
            ignores += 1

    elif event[0] == 2:
        ignores += overtaking_forbidden
        overtaking_forbidden = 0

    elif event[0] == 3:
        if current_speed > event[1]:
            ignores += 1
        else:
            max_speeds.append(event[1])

    elif event[0] == 4:
        overtaking_forbidden = 0
    elif event[0] == 5:
        max_speeds = []
    elif event[0] == 6:
        overtaking_forbidden += 1

print(ignores)



import sys
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
c2 = 0
speed = 0
c = 0
st = []

for i in a:
    if i[0] == 1:
        speed = i[1]
        while len(st) and st[-1] < speed:
            c += 1
            st.pop()
    elif i[0] == 2 and c2 > 0:
        c += c2
        c2 = 0
    elif i[0] == 3:
        st.append(i[1])
        while len(st) and st[-1] < speed:
            c += 1
            st.pop()
    elif i[0] == 4:
        c2 = 0
    elif i[0] == 5:
        st = []
    elif i[0] == 6:
        c2 += 1
print(c)
        


n=int(input())
s=0
ms=[]
o=1
z=0
q=0
for _ in range(n):
    a=list(map(int,input().split()))
    if a[0]==1:
        s=a[1]
        while len(ms)>0 and ms[-1]<s:
            z+=1
            ms.pop(-1)
    elif a[0]==2:
        if o!=1:
            z+=(1-o)
            o=1
    elif a[0]==3:
        ms.append(a[1])
        while len(ms)>0 and ms[-1]<s:
            z+=1
            ms.pop(-1)
    elif a[0]==4:
        o=1
    elif a[0]==5:
        ms=[]
    elif a[0]==6:
        o-=1
print(z)

from collections import deque

n = int(input())
speed_stack = deque()
over_take = 0
speed = 0
penalty = 0
def speed_check(penalty, x):
    while speed_stack and speed_stack[-1] < x:
        speed_stack.pop()
        penalty += 1
    return penalty

for i in range(n):
    update = [int(j) for j in input().split()]
    if update[0] == 1:
        speed = update[1]
        penalty = speed_check(penalty, speed)
    elif update[0] == 2:
        penalty += over_take
        over_take = 0
    elif update[0] == 3:
        speed_stack.append(update[1])
        penalty = speed_check(penalty, speed)
    elif update[0] == 4:
        over_take = 0
    elif update[0] == 5:
        speed_stack = deque()
    else:
        over_take += 1
print(penalty)

#!/usr/bin/env python3
from sys import stdin, stdout
from math import inf

def rint():
    return list(map(int, stdin.readline().split()))
#lines = stdin.readlines()

csp = 0
ov = 1

sign = []
ans = 0
spst = [inf]
ovst = [1]
n = int(input())
for i in range(n):
    sign = list(rint())
    if sign[0] == 1:
        csp = sign[1]
        while csp > spst[-1]:
            spst.pop()
            ans += 1
    if sign[0] == 2:
        while ovst[-1] == 0:
            ovst.pop()
            ans += 1
    if sign[0] == 3:
        if sign[1] >= csp:
            spst.append(sign[1])
        else:
            ans += 1
    if sign[0] == 4:
        ovst.append(1)
    if sign[0] == 5:
        spst.append(inf)
    if sign[0] == 6:
        ovst.append(0)
print(ans)


MAX_SPEED = 300
n = int(input())
actions = [list(map(int, input().split())) for _ in range(n)]
speed = [0 for i in range(n)]

for i in range(n):
    if actions[i][0] == 1:
        lastSpeed = actions[i][1]
    speed[i] = lastSpeed    

ignored = 0
overtook = False
maxSpeed = -1
for i in range(n-1,-1,-1):
    maxSpeed = max(maxSpeed, speed[i])
    if actions[i][0] == 1:
        pass
    elif actions[i][0] == 2:
        overtook = True
    elif actions[i][0] == 3:
        if maxSpeed > actions[i][1]:
            ignored += 1
        else:
            maxSpeed = speed[i-1]
    elif actions[i][0] == 4:
        overtook = False
    elif actions[i][0] == 5:
        maxSpeed = speed[i-1]
    elif actions[i][0] == 6:
        if overtook:
            ignored += 1
  
print(ignored)


n = int(input())

no_overtake = 0
speedlimit = [float("inf")]

speed = 0

vio = 0

ot_ignored = False

for _ in range(n):
    cmd = input().split()
    if cmd[0] == '1':
        speed = int(cmd[1])
        while speed > speedlimit[-1]:
            vio += 1
            speedlimit.pop(-1)
    elif cmd[0] == '2':
        if no_overtake and not ot_ignored:
            vio += no_overtake
            ot_ignored = True
    elif cmd[0] == '3':
        speedlimit.append(int(cmd[1]))
        if speed > speedlimit[-1]:
            vio += 1
            speedlimit.pop(-1)
    elif cmd[0] == '4':
        no_overtake = 0
        ot_ignored = False
    elif cmd[0] == '5':
        speedlimit = [float('inf')]
    elif cmd[0] == '6':
        if ot_ignored == 1:
            no_overtake = 0
        no_overtake += 1
        ot_ignored = False

print(vio)


n = int(input())

no_overtake = 0
speedlimit = [float("inf")]

speed = 0

vio = 0

ot_ignored = False

for _ in range(n):
    cmd = input().split()
    if cmd[0] == '1':
        speed = int(cmd[1])
        while speed > speedlimit[-1]:
            vio += 1
            speedlimit.pop(-1)
    elif cmd[0] == '2':
        if no_overtake and not ot_ignored:
            vio += no_overtake
            ot_ignored = True
    elif cmd[0] == '3':
        speedlimit.append(int(cmd[1]))
        if speed > speedlimit[-1]:
            vio += 1
            speedlimit.pop(-1)
    elif cmd[0] == '4':
        no_overtake = 0
        ot_ignored = False
    elif cmd[0] == '5':
        speedlimit = [float('inf')]
    elif cmd[0] == '6':
        if ot_ignored == 1:
            no_overtake = 0
        no_overtake += 1
        ot_ignored = False

print(vio)



n, d = int(input()), []
for i in range(n):
    d.append(tuple(map(int, input().split())))
st, c = [], 0
ans, v = 0, 0
for i in d:
    if i[0] == 1:
        v = i[1]
        while(len(st) != 0 and st[-1] < v):
            ans += 1
            st.pop()
    elif i[0] == 2:
        ans += c
        c = 0
    elif i[0] == 3:
        if v > i[1]:
            ans += 1
        else:
            st.append(i[1])
    elif i[0] == 4:
        c = 0
    elif i[0] == 5:
        st.clear()
    elif i[0] == 6:
        c += 1
print(ans)
    


def Task485D():
    cur_speed   = 0
    is_outstrip = 0
    err_count   = 0
    event_speed_limit = []

    event_count = int(input())

    for a in range(event_count):
        ev =list(map(int, input().split()))
        if ev[0] == 1:
            cur_speed = ev[1]
            while len(event_speed_limit) > 0:
                if cur_speed > event_speed_limit[len(event_speed_limit) - 1]:
                    event_speed_limit.pop()
                    err_count += 1
                else:
                    break

        if ev[0] == 2:
            err_count += is_outstrip
            is_outstrip = 0

        if ev[0] == 3:
            if cur_speed > ev[1]:
                err_count += 1
            else:
                event_speed_limit.append(ev[1])

        if ev[0] == 4:
            is_outstrip = 0

        if ev[0] == 5:
            event_speed_limit.clear()

        if ev[0] == 6:
            is_outstrip += 1

    print(err_count)

def __starting_point():
    Task485D()
__starting_point()

n=int(input())
over=[]
speed=[]
for x in range(n):
    ab=input()
    if(len(ab)>1):
        a,b=ab.split()
    else:
        a=ab
    if(a=='1' or a=='3' or a=='5'):
        if(a=='5'):
            speed.append([5,2**31-1])
        else:
            speed.append([int(a),int(b)])
    else:
        over.append(int(a))
overignore=0
oversum=0
i=len(over)-1
while(i>=0):
    if(over[i]==2):
        oversum+=1
    elif(over[i]==4):
        oversum=0
    if(over[i]==6 and oversum>0):
        overignore+=1
    i-=1

curspeed=[]
stack=[]
stack.append(2**31-1)
i=0
while(i<len(speed)):
    if(speed[i][0]==1):
        curspeed.append(speed[i][1])
    else:
        curspeed.append(curspeed[-1])
        stack.append(speed[i][1])
    i+=1

speedignore=0
i=len(speed)-1
while(i>=0):
    if(speed[i][0]!=1 and speed[i][1]==stack[-1]):
        if(curspeed[i]>stack[-1]):
            stack.pop()
            speedignore+=1
        else:   
            stack.pop()
            i-=1
    else:
        if(curspeed[i]>stack[-1]):
            stack.pop()
            speedignore+=1
        else:
            i-=1
print(speedignore+overignore)

num=int(input())
slimit = []
ans = 0
last = 0
olimit = 1
for i in range(num):
    a = list( map(int , input().split()) )
    if a[0] == 1:
        last = a[1]
        while len(slimit)>0 and slimit[-1] < last:
            ans+=1
            slimit.pop(-1)
    elif a[0] == 2:
        if olimit!=1:
            ans+=(1-olimit)
            olimit = 1
    elif a[0] == 3:
        slimit.append(a[1])
        while len(slimit)>0 and slimit[-1] < last:
            ans+=1
            slimit.pop(-1)
    elif a[0] == 4:
        olimit = 1
    elif a[0] == 5:
        slimit = []
    elif a[0] == 6:
        olimit -= 1
print(ans)

n=int(input())
st=[0]*n
top=-1
curr_speed=0
ans=0
c=0
while(n>0):
    
    n-=1
    a=list(map(int,input().split()))
    if(a[0]==1):
        curr_speed=a[1]
        if(top>=0 and curr_speed>st[top]):
            while(curr_speed>st[top] and top>=0):
                top-=1
                ans+=1
        
    if(a[0]==4):
        c=0
    elif(a[0]==6):
        c+=1
    if(a[0]==5):
        top=-1
        
    if(a[0]==2):
        ans+=c
        c=0
    if(a[0]==3):
        if(curr_speed>a[1]):
            ans+=1
        else:
            st[top+1]=a[1]
            top+=1
print(ans)


import collections
n=int(input())
x,v=map(int,input().split())
o=collections.deque()
q=0
k=0
for i in range(1,n):
    s=input()
    if s[0]=='1':
        x,v=map(int,s.split())
        while len(o)>0 and o[-1]<v:
            o.pop()
            k+=1
    if s[0]=='2':
        k+=q
        q=0
    if s[0]=='3':
        x,y=map(int,s.split())
        if v<=y:
            o.append(y)
        else:
            k+=1
    if s[0]=='4':
        q=0
    if s[0]=='5':
        o.clear()
    if s[0]=='6':
        q+=1
print(k)

n = int(input())
v = int(input()[2:])
p = [1e9]
d = k = 0
for i in range(n - 1):
    s = input()
    t = int(s[0])
    if t == 1:
        v = int(s[2:])
        while p[-1] < v:
            p.pop()
            k += 1
    if t == 2:
        k += d
        d = 0
    if t == 3:
        u = int(s[2:])
        if v > u: k += 1
        else: p.append(u)
    if t == 4: d = 0
    if t == 5: p = [1e9]
    if t == 6: d += 1
print(k)

p = [1e9]
d = k = v = 0
for i in range(int(input())):
    s = input()
    t = int(s[0])
    if t == 1:
        v = int(s[2:])
        while p[-1] < v:
            p.pop()
            k += 1
    if t == 2: k, d = k + d, 0
    if t == 3:
        u = int(s[2:])
        if v > u: k += 1
        else: p += [u]
    if t == 4: d = 0
    if t == 5: p = [1e9]
    if t == 6: d += 1
print(k)

n = int(input())
speed = 0
overtake_allowed = True
just_overtake_violation = False
speed_signs = []
violations = 0
overtake_passed = 0
for i in range(n):
	arr = [int(a) for a in input().split(' ')]

	if arr[0] == 1:
		if arr[1] > speed:
			speed = arr[1]
			for j in range(len(speed_signs) - 1, -1, -1):
				if speed > speed_signs[j]:
					violations += 1
					del speed_signs[j]
				else:
					break
		else:
			speed = arr[1]

	if arr[0] == 2:
		violations += overtake_passed
		overtake_passed = 0

	if arr[0] == 3:
		if speed > arr[1]:
			violations += 1
		else:
			speed_signs.append(arr[1])

	if arr[0] == 4:
		overtake_passed = 0

	if arr[0] == 5:
		speed_signs = []

	if arr[0] == 6:
		overtake_passed += 1
# print(speed_signs)
# print(violated_speed)
# print(overtake_signs)
# print(violated_overtake)
print(violations)

import math
from collections import Counter, defaultdict
from itertools import accumulate

R = lambda: map(int, input().split())
n = int(input())
spl = [math.inf]
ot_forbid = 0
sp = 0
res = 0
for i in range(n):
    arr = list(R())
    tp = arr[0]
    if tp == 1:
        sp = arr[1]
        while sp > spl[-1]:
            spl.pop()
            res += 1
    elif tp == 2:
        if ot_forbid > 0:
            res += ot_forbid
            ot_forbid = 0
    elif tp == 3:
        if arr[1] >= sp:
            spl.append(arr[1])
        else:
            res += 1
    elif tp == 4:
        ot_forbid = 0
    elif tp == 5:
        spl = [math.inf]
    else:
        ot_forbid += 1
print(res)