import bisect n = int(input()) a = int(input()) b_list = list(map(int, input().split())) c = int(input()) d_list = list(map(int, input().split())) original_A = sorted(b_list) original_C = sorted(d_list) current_A = [] current_C = [] wins = 0 for _ in range(n): # Refill if current cards are exhausted if not current_A: current_A = list(original_A) if not current_C: current_C = list(original_C) sorted_A = sorted(current_A) sorted_C = sorted(current_C) found = False chosen_d = None chosen_a = None # Find the smallest d in C that can be beaten by some a in A for d in sorted_C: idx = bisect.bisect_right(sorted_A, d) if idx < len(sorted_A): chosen_d = d chosen_a = sorted_A[idx] found = True break if found: wins += 1 current_A.remove(chosen_a) current_C.remove(chosen_d) else: # Remove smallest d and smallest a as they can't contribute to future wins # Since any pair will not result in a win, we just remove the smallest ones if sorted_C and sorted_A: current_A.remove(sorted_A[0]) current_C.remove(sorted_C[0]) else: # This case should not occur due to refilling logic pass print(wins)