import heapq n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ls = [list(map(int, input().split())) for _ in range(m)] def solve(n, m, a, b, ls): a2 = [[a[i], i] for i in range(n)] b2 = [[b[i], i] for i in range(n)] a2.sort(key = lambda x: x[0]) b2.sort(key = lambda x: x[0]) ls.sort(key = lambda x: x[1]) flg = [0] * n h1 = [] h2 = [] idxa = 0 idxb = 0 for t, c in ls: while idxa < n: if a2[idxa][0] > c: break idx = a2[idxa][1] heapq.heappush(h1, (-b[idx])*n+idx) idxa += 1 while idxb < n: if b2[idxb][0] > c: break idx = b2[idxb][1] heapq.heappush(h2, (-a[idx])*n+idx) idxb += 1 if t: while h2: tmp = heapq.heappop(h2) x, i = divmod(tmp, n) if flg[i] == 0: flg[i] = 1 break else: while h1: tmp = heapq.heappop(h1) x, i = divmod(tmp, n) if flg[i] == 0: flg[i] = 1 break # print(flg, t, c) # print(h1, h2) return n - sum(flg) # print(flg) from itertools import permutations def naive(n, m, a, b, ls): ans = n for p in permutations(range(m)): res = n for i in range(m): t, c = ls[i] if t: res -= b[p[i]] <= c else: res -= a[p[i]] <= c ans = min(ans, res) return ans print(solve(n, m, a, b, ls)) # print(naive(n, m, a, b, ls)) exit() import random for _ in range(100): n, m = random.randint(1, 5), random.randint(1, 5) # m = max(m, n) a = [random.randint(1, 10**9) for _ in range(n)] b = [random.randint(1, 10**9) for _ in range(n)] ls = [[random.randint(0, 1), random.randint(1, 10**9)] for _ in range(m)] solve(n, m, a, b, ls) continue if solve(n, m, a, b, ls) != naive(n, m, a, b, ls): print(n, m) print(*a) print(*b) for t, c in ls: print(t, c) break