import itertools n = int(input()) a = int(input()) b = list(map(int, input().split())) c = int(input()) d = list(map(int, input().split())) # Compute count[x][y] count = [[0 for _ in range(c)] for _ in range(a)] for i in range(n): x = i % a y = i % c count[x][y] += 1 max_win = 0 # Generate all permutations of B and D for b_perm in itertools.permutations(b): for d_perm in itertools.permutations(d): current = 0 for i in range(n): x = i % a y = i % c if b_perm[x] > d_perm[y]: current += 1 if current > max_win: max_win = current print(max_win)