n = int(input()) a = int(input()) b = list(map(int, input().split())) c = int(input()) d = list(map(int, input().split())) b.sort() d.sort() i = 0 j = 0 max_wins = 0 # Compute the maximum wins using two-pointer technique while i < a and j < c: if b[i] > d[j]: max_wins += 1 i += 1 j += 1 else: i += 1 # The total number of wins is the maximum number of times the optimal pairs can be repeated within N matches total = (n // max_wins) * max_wins if max_wins != 0 else 0 print(total)