On a chessboard with a width of $10^9$ and a height of $10^9$, the rows are numbered from bottom to top from $1$ to $10^9$, and the columns are numbered from left to right from $1$ to $10^9$. Therefore, for each cell of the chessboard you can assign the coordinates $(x,y)$, where $x$ is the column number and $y$ is the row number.

Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates $(1,1)$. But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number $10^9$).

Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:   Vertical. Each of these is defined by one number $x$. Such spells create an infinite blocking line between the columns $x$ and $x+1$.  Horizontal. Each of these is defined by three numbers $x_1$, $x_2$, $y$. Such spells create a blocking segment that passes through the top side of the cells, which are in the row $y$ and in columns from $x_1$ to $x_2$ inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells. 

 [Image] 

 An example of a chessboard. 

Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell $(r_0,c_0)$ into the cell $(r_1,c_1)$ only under the condition that $r_1 = r_0$ or $c_1 = c_0$ and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).

Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!


-----Input-----

The first line contains two integers $n$ and $m$ ($0 \le n,m \le 10^5$) — the number of vertical and horizontal spells.

Each of the following $n$ lines contains one integer $x$ ($1 \le x < 10^9$) — the description of the vertical spell. It will create a blocking line between the columns of $x$ and $x+1$.

Each of the following $m$ lines contains three integers $x_1$, $x_2$ and $y$ ($1 \le x_{1} \le x_{2} \le 10^9$, $1 \le y < 10^9$) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number $y$, in columns from $x_1$ to $x_2$ inclusive.

It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.


-----Output-----

In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell $(1,1)$ to at least one cell in the row with the number $10^9$


-----Examples-----
Input
2 3
6
8
1 5 6
1 9 4
2 4 2

Output
1
Input
1 3
4
1 5 3
1 9 4
4 6 6

Output
1
Input
0 2
1 1000000000 4
1 1000000000 2

Output
2
Input
0 0

Output
0
Input
2 3
4
6
1 4 3
1 5 2
1 6 5

Output
2


-----Note-----

In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.

 [Image] Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home. 

In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.

 $m$ Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home. 

In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.

 [Image] Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home. 

In the fourth sample, we have no spells, which means that we do not need to remove anything.

In the fifth example, we can remove the first vertical and third horizontal spells.

 [Image] Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Amortized analysis


Solutions:
(n, m) = map(int, input().split())
ver = [int(input()) for _ in range(n)]
ver.append(10 ** 9)
hor = []
for _ in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 == 1:
		hor.append(x2)
hor.sort()
ver.sort()
j = 0
ans = 10 ** 18
for i in range(n + 1):
	while j < len(hor) and ver[i] > hor[j]:
		j += 1
	ans = min(ans, i + len(hor) - j)
	if j == len(hor):
		break
print(ans)


(n, m) = map(int, input().split())
cols = []
for i in range(n):
	cols.append(int(input()))
rows = []
for i in range(m):
	k = list(map(int, input().split()))
	if k[0] == 1:
		rows.append(k[1])
ans = n + m
cols.sort()
rows.sort()
cols.append(int(1000000000.0))
j = 0
rem = 0
for i in cols:
	while j < len(rows) and rows[j] < i:
		j += 1
	ans = min(ans, len(rows) - j + rem)
	rem += 1
print(ans)


(n, m) = map(int, input().split())
vert = [0 for i in range(n)]
for i in range(n):
	vert[i] = int(input())
vert.sort()
horiz = []
addres = 0
for i in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 > 1:
		continue
	if x2 == 10 ** 9:
		addres += 1
		continue
	horiz += [x2]
horiz.sort()
res = min(n, len(horiz))
(i, j) = (0, 0)
while j < n:
	while i < len(horiz) and horiz[i] < vert[j]:
		i += 1
	res = min(res, len(horiz) - i + j)
	j += 1
print(res + addres)


import bisect
(n, m) = map(int, input().split())
ar1 = [1] + [int(input()) for _ in range(n)]
ar1.append(10 ** 9)
ar1.sort()
ar2 = [list(map(int, input().split())) for _ in range(m)]
kek = list()
for x in ar2:
	j1 = bisect.bisect_left(ar1, x[0])
	j2 = bisect.bisect_right(ar1, x[1])
	if x[0] == 1:
		kek.append(j2)
res = [0] * (len(ar1) + 1)
res[0] = len(kek)
for x in kek:
	res[x] -= 1
for i in range(1, len(res)):
	res[i] += res[i - 1]
min_ = float('inf')
for i in range(1, len(res) - 1):
	min_ = min(min_, res[i] + i - 1)
print(min_)


from bisect import bisect_right as br
from bisect import bisect_left as bl
MAX = 10 ** 9

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)
(n, m) = map(int, input().split())
v = []
h = []
for _ in range(n):
	v.append(int(input()))
for _ in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 == 1:
		h.append(x2)
lh = len(h)
h.sort()
v.sort()
if not lh:
	print(0)
elif n == 0:
	print(lh - bl(h, MAX))
else:
	mn = n + lh - bl(h, MAX)
	for i in range(n):
		mn = min(mn, lh - bl(h, v[i]) + i)
	print(mn)


import sys
(v, h) = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))
vs = []
hs = []
for i in range(v):
	vs.append(int(sys.stdin.readline()))
vs.sort()
vs.append(10 ** 9)
for i in range(h):
	(x1, x2, y) = list(map(lambda x: int(x), sys.stdin.readline().split(' ')))
	if x1 == 1:
		hs.append([x1, x2, y])

def sort_x2(val):
	return val[1]
hs.sort(key=sort_x2)
hsl = len(hs)
vsl = len(vs)
res = v + h
hi = 0
for (vi, v) in enumerate(vs, start=0):
	while hi < hsl and hs[hi][1] < v:
		hi += 1
	res = min(res, vi + hsl - hi)
print(res)


(n, m) = map(int, input().split())
sir = [int(input()) for i in range(n)]
sir.append(10 ** 9)
w = []
b = 0
mini = 999999
nrSir = len(sir)
for i in range(m):
	(x, y, z) = map(int, input().split())
	if x == 1:
		w.append(y)
sir.sort()
w.sort()
nrW = len(w)
for i in range(nrSir):
	while b < nrW and sir[i] > w[b]:
		b = b + 1
	if mini + b > nrW + i:
		mini = i - b + nrW
	if b == nrW:
		break
print(mini)


from bisect import bisect
(n, m) = map(int, input().split())
vv = sorted([int(input()) for _ in range(n)])
hh = [0] * n
rr = 0
for _ in range(m):
	(one, x, _) = map(int, input().split())
	if one == 1:
		if x == 1000000000:
			rr += 1
		else:
			ind = bisect(vv, x)
			if ind:
				hh[ind - 1] += 1
r = n
s = 0
for (i, h) in reversed(list(enumerate(hh))):
	s += h
	r = min(r, s + i)
print(r + rr)


(n, m) = map(int, input().split())
V = []
for i in range(n):
	V.append(int(input()))
V.sort()
V.append(10 ** 9)
n += 1
X2 = []
for i in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 == 1:
		X2.append(x2)
X2.sort()
k = len(X2)
i = 0
j = 0
ans = 10 ** 9 + 7
c = 0
while i < n:
	while j < k:
		if X2[j] < V[i]:
			c += 1
			j += 1
		else:
			break
	ans = min(ans, k - c + i)
	i += 1
print(ans)


from bisect import bisect_left

def readint():
	return int(input())

def readline():
	return [int(c) for c in input().split()]

def main():
	MAX = 10 ** 9
	(n, m) = readline()
	v = sorted([readint() for _ in range(n)])
	h = []
	for _ in range(m):
		(x1, x2, _) = readline()
		if x1 == 1:
			h.append(x2)
	h.sort()
	lh = len(h)
	if lh == 0:
		print(0)
	elif n == 0:
		print(lh - bisect_left(h, MAX))
	else:
		mn = n + lh - bisect_left(h, MAX)
		for i in range(n):
			mn = min(mn, lh - bisect_left(h, v[i]) + i)
		print(mn)
main()


import sys
(n, m) = map(int, sys.stdin.readline().split())
X = [int(sys.stdin.readline()) for i in range(n)]
Y = [list(map(int, sys.stdin.readline().split())) for i in range(m)]
Z = []
ANS = 0
for y in Y:
	if y[0] == 1 and y[1] == 10 ** 9:
		ANS += 1
	elif y[0] == 1:
		Z.append(y[1])
X.sort(reverse=True)
Z.sort(reverse=True)
XCOUNT = [0] * n
i = 0
j = 0
l = len(Z)
X.append(0)
Z.append(0)
while i < l + 1 and j < n:
	if Z[i] >= X[j]:
		i += 1
	else:
		XCOUNT[j] = i
		j += 1
count = n
XCOUNT.reverse()
for i in range(n):
	if count > i + XCOUNT[i]:
		count = i + XCOUNT[i]
print(count + ANS)


from math import *
import sys
input = sys.stdin.readline

def bin_search(arr, n):
	pos = -1
	for i in range(35, -1, -1):
		jump = 1 << i
		if pos + jump >= len(arr):
			continue
		if arr[pos + jump] <= n - 1:
			pos += jump
	return len(arr) - pos - 1

def main():
	(n, m) = [int(x) for x in input().split(' ')]
	vert = []
	for i in range(n):
		vert.append(int(input()))
	hor = []
	for i in range(m):
		(col1, col2, row) = [int(x) for x in input().split(' ')]
		if col1 != 1:
			continue
		hor.append(col2)
	vert.append(1000000000)
	vert = sorted(vert)
	hor = sorted(hor)
	best = int(10000000000.0)
	for i in range(len(vert)):
		cur_ans = bin_search(hor, vert[i]) + i
		best = min(best, cur_ans)
	print(best)
main()


from collections import namedtuple
import sys
HS = namedtuple('HS', 'x1 x2 y')
(n, m) = [int(w) for w in input().split()]
vs = [int(input()) for _ in range(n)]
hs = [HS(*[int(w) for w in input().split()]) for _ in range(m)]
vs.sort()
hr = len([s for s in hs if s.x1 == 1 and s.x2 == 10 ** 9])
hs = [s.x2 for s in hs if s.x1 == 1 and s.x2 < 10 ** 9]
hs.sort()
r = hc = len(hs)
hi = vi = 0
for hi in range(hc):
	while vi < n and hs[hi] >= vs[vi]:
		vi += 1
	c = hc - hi - 1 + vi
	if c < r:
		r = c
print(r + hr)


(n, m) = map(int, input().split())
vertical_blocks = []
for i in range(n):
	vertical_blocks.append(int(input()))
vertical_blocks.append(10 ** 9)
horizontal_blocks = []
for i in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 == 1:
		horizontal_blocks.append(x2)
vertical_blocks.sort()
horizontal_blocks.sort()
(pointer, res) = (0, n + m)
for i in range(len(vertical_blocks)):
	while pointer < len(horizontal_blocks) and horizontal_blocks[pointer] < vertical_blocks[i]:
		pointer += 1
	res = min(res, i + len(horizontal_blocks) - pointer)
print(res)


(n, m) = map(int, input().split())
vert = []
for i in range(n):
	v = int(input())
	vert.append(v)
horz = []
for i in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 == 1:
		horz.append(x2)
vert.sort()
horz.sort()
vert.append(1000000000)

def next(k, a, x):
	while k < len(a) and a[k] < x:
		k += 1
	return k
num = next(0, horz, vert[0])
ans = len(horz) - num
for i in range(1, len(vert)):
	num2 = next(num, horz, vert[i])
	t = i + len(horz) - num2
	if t < ans:
		ans = t
	num = num2
print(ans)


(m, n) = map(int, input().split())
a = [int(input()) for q in range(m)]
a.append(10 ** 9)
s = []
for q in range(n):
	(f, g, d) = map(int, input().split())
	if f == 1:
		s.append(g)
a.sort()
s.sort()
q1 = 0
min1 = float('inf')
for q2 in range(len(a)):
	while q1 < len(s) and a[q2] > s[q1]:
		q1 += 1
	if min1 > q2 + len(s) - q1:
		min1 = q2 + len(s) - q1
	if q1 == len(s):
		break
print(min1)


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def main():
	(n, m) = mints()
	vert = []
	horiz = []
	for i in range(n):
		x = mint()
		vert.append(x)
	vert.sort()
	for i in range(m):
		(x1, x2, y) = mints()
		horiz.append((y, x1, x2))
	horiz.sort()
	p = -1
	hh = []
	for i in horiz:
		if p != i[0]:
			p = i[0]
			if i[1] == 1:
				hh.append(i[2])
	hh.sort()
	i = 0
	hl = len(hh)
	vl = len(vert)
	r = n + m
	for j in range(vl):
		while i < hl and hh[i] < vert[j]:
			i += 1
		r = min(r, hl - i + j)
	while i < hl and hh[i] < 1000000000:
		i += 1
	r = min(r, hl - i + vl)
	print(r)
main()


val = 10 ** 9
(n, m) = map(int, input().split())
arr1 = []
for i in range(n):
	x = int(input())
	arr1.append(x)
arr1.append(val)
arr2 = []
ans = val
finalval = 0
arr1.sort()
for i in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 == 1:
		if x2 == val:
			finalval += 1
		elif len(arr1) > 0 and x2 >= arr1[0]:
			arr2.append(x2)
arr2.sort()
i = 0
j = 0
while i < len(arr1) and j < len(arr2):
	if arr1[i] > arr2[j]:
		j += 1
	elif arr1[i] == arr2[j]:
		temp1 = len(arr2) - j
		ans = min(i + temp1, ans)
		i += 1
	else:
		temp1 = len(arr2) - j
		ans = min(i + temp1, ans)
		i += 1
ans = min(i, ans)
print(ans + finalval)


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
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')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(n, m) = map(int, input().split())
l = []
l1 = []
for i in range(n):
	k = int(input())
	l.append(k)
l.append(10 ** 9)
for i in range(m):
	(a, b, c) = map(int, input().split())
	l1.append((b, a, c))
l.sort(reverse=True)
l1.sort(reverse=True)
t = 0
ans = n + m
c = 0
for i in range(n + 1):
	while True:
		if t == m:
			break
		if l1[t][0] >= l[i]:
			if l1[t][1] == 1:
				c += 1
			t += 1
		else:
			break
	ans = min(ans, n - i + c)
print(ans)


(n, m) = map(int, input().split())
y = []
for i in range(n):
	y.append(int(input()))
y.append(10 ** 9)
x = []
for i in range(m):
	(a, b, c) = map(int, input().split())
	if a == 1:
		x.append(b)
y.sort()
x.sort()
m = len(x)
ans = m
k = 0
for i in range(n + 1):
	ok = True
	for j in range(k, m):
		if y[i] <= x[j]:
			k = j
			ok = False
			break
	if ok:
		k = m
		ans = min(ans, m - k + i)
		break
	ans = min(ans, m - k + i)
print(ans)


(n, m) = map(int, input().split())
x = [0] * (n + 1)
for i in range(n):
	x[i] = int(input())
x[n] = 1000000000
vert = []
for i in range(m):
	(x1, x2, y) = map(int, input().split())
	if x1 == 1:
		vert.append(x2)
vert.sort()
x.sort()
cur = 0
minicount = n + m
k = len(vert)
for i in range(n + 1):
	while cur < k:
		if x[i] <= vert[cur]:
			break
		cur += 1
	minicount = min(minicount, k - cur + i)
print(minicount)


(n, m) = [int(x) for x in input().split()]
v = []
h = []
for i in range(n):
	x = int(input())
	v.append(x)
for i in range(m):
	(x, y, z) = [int(x) for x in input().split()]
	if x == 1:
		h.append(y)
h.sort()
v.sort()
m = len(h)
n = len(v)
if n == 0 or v[n - 1] != 1000000000:
	v.append(1000000000)
	n += 1
mina = 9999999999999
j = 0
for i in range(n):
	while j < m and h[j] < v[i]:
		j += 1
	mina = min(mina, i + m - j)
print(mina)


l = input().split()
n = int(l[0])
m = int(l[1])
vs = []
for i in range(n):
	vs.append(int(input()))
vs.append(10 ** 9)
vs.sort()
hs = []
for i in range(m):
	l = input().split()
	if int(l[0]) == 1:
		hs.append(int(l[1]))
hs.sort()
j = 0
mina = 10 ** 9
for i in range(n + 1):
	if j < len(hs):
		while hs[j] < vs[i]:
			j += 1
			if j == len(hs):
				break
	if i + len(hs) - j < mina:
		mina = i + len(hs) - j
print(mina)


(n, m) = map(int, input().split())
vert = []
for i in range(n):
	vert.append(int(input()))
horiz = []
for i in range(m):
	(a, b, c) = map(int, input().split())
	if a == 1:
		horiz.append(b)
vert.sort()
vert.append(1000000000)
vert.append(2000000000)
horiz.sort()
oof = [0] * (n + 2)
b = 0
for i in range(len(horiz)):
	while True:
		if horiz[i] < vert[b]:
			oof[b] += 1
			break
		else:
			b += 1
mini = 1000000
bad = len(horiz)
for i in range(n + 1):
	bad -= oof[i]
	if bad + i < mini:
		mini = bad + i
print(mini)


import collections
import bisect
(N, M) = map(int, input().split())
vlines = []
for i in range(N):
	x = int(input())
	vlines.append(x)
vlines.sort()
vlines.append(10 ** 9)
yxs = collections.defaultdict(list)
ys = set()
for i in range(M):
	(l, r, y) = map(int, input().split())
	yxs[y].append((l, r))
	if l <= 1:
		ys.add(y)

def merge(segs):
	segs.sort()
	ans = [segs[0]]
	for s in segs[1:]:
		pre = ans[-1]
		if s[0] > pre[1]:
			return ans[0]
		else:
			ans[-1] = (pre[0], s[1])
	return ans[0]
xs = [merge(yxs[y])[1] for y in ys]
xs.sort()
ans = float('inf')
for (i, x) in enumerate(vlines):
	if i >= ans:
		break
	count = i + len(xs) - bisect.bisect_left(xs, x)
	ans = min(ans, count)
print(ans)


import sys
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65
MAXRIGHT = 10 ** 9

def getInt():
	return int(sys.stdin.readline().rstrip())

def getInts():
	return map(int, sys.stdin.readline().rstrip().split(' '))

def getString():
	return sys.stdin.readline().rstrip()

def printOutput(ans):
	sys.stdout.write()
	pass

def solve(v, h):
	vertdict = []
	h.sort()
	v.sort()
	hcur = 0
	for vert in v:
		while hcur < len(h) and h[hcur][1] < vert:
			hcur += 1
		vertdict.append(len(h) - hcur)
	minmoves = vertdict[0]
	for i in range(len(vertdict)):
		minmoves = min(minmoves, vertdict[i] + i)
	return minmoves

def readinput():
	(v, h) = getInts()
	vert = []
	horiz = []
	for _ in range(v):
		vert.append(getInt())
	vert.append(MAXRIGHT)
	for _ in range(h):
		(x1, x2, y) = getInts()
		if x1 == 1:
			horiz.append((x1, x2, y))
	print(solve(vert, horiz))
readinput()


import sys
input = sys.stdin.readline

def bs(num):
	low = 0
	high = len(rows) - 1
	while low < high:
		mid = (low + high) // 2
		if rows[mid] >= num:
			high = mid - 1
		else:
			low = mid + 1
	if rows[low] < num:
		return len(rows) - (low + 1)
	else:
		return len(rows) - low
(n, m) = map(int, input().split())
if n == 0 and m == 0:
	print(0)
	exit(0)
col = []
rows = []
for i in range(n):
	col.append(int(input()))
col.append(10 ** 9)
for i in range(m):
	temp = list(map(int, input().split()))
	if temp[0] == 1:
		rows.append(temp[1])
col.sort()
rows.sort()
ans = 10 ** 9
if len(rows) == 0:
	print(0)
	exit(0)
for i in range(n + 1):
	count1 = bs(col[i])
	ans = min(ans, count1 + i)
print(ans)