def read_data(): N = int(input()) A = int(input()) Bs = list(map(int, input().split())) C = int(input()) Ds = list(map(int, input().split())) return N, A, Bs, C, Ds def solve(N, A, Bs, C, Ds): Bs.sort() Ds.sort() minA = min(Bs) maxA = max(Bs) minC = min(Ds) maxC = max(Ds) if minA > maxC: return N if maxA < minC: return 0 if C >= A: return solve_C_is_longer(N, A, Bs, C, Ds) else: return solve_A_is_longer(N, A, Bs, C, Ds) def solve_C_is_longer(N, A, Bs, C, Ds): repeat, residue = divmod(N, C) Cs = Ds * repeat + Ds[:residue] repeat, residue = divmod(N, A) n_win = 0 for r in range(repeat): subCs = Cs[r*A:(r+1)*A] n_win += count_wins(Bs, subCs) if residue: subCs = Cs[-residue:] subAs = Bs[-residue:] n_win += count_wins(subAs, subCs) return n_win def solve_A_is_longer(N, A, Bs, C, Ds): Bs.sort(reverse=True) repeat, residue = divmod(N, A) As = Bs * repeat + Bs[:residue] repeat, residue = divmod(N, C) n_win = 0 for r in range(repeat): subAs = As[r*A:(r+1)*A] n_win += count_wins(subAs, Ds) if residue: subAs = As[-residue:] subCs = Ds[:residue] n_win += count_wins(subAs, subCs) return n_win def count_wins(As, Cs): N = len(As) Acopy = As[:] Ccopy = Cs[:] Acopy.sort() Ccopy.sort() idxA = 0 n_win = 0 for c in Ccopy: while Acopy[idxA] <= c: idxA += 1 if idxA == N: return n_win n_win += 1 return n_win N, A, Bs, C, Ds = read_data() print(solve(N, A, Bs, C, Ds))