import bisect def makediv(n): lower_divisors , upper_divisors = [], [] i = 1 while i*i <= n: if n % i == 0: lower_divisors.append(i) if i != n // i: upper_divisors.append(n//i) i += 1 return lower_divisors + upper_divisors[::-1] n, m = map(int,input().split()) a = list(map(int,input().split())) b = list(map(int,input().split())) a2 = [a[i] * a[i] for i in range(n)] b2 = [b[i] * b[i] for i in range(m)] if a[0] != 1 or b[0] != 1: print(1) exit() c = [0] * 40000 for i in range(n): c[a[i]] = 1 for i in range(m): c[b[i]] = 1 v = 0 for i in range(1, 40000): if c[i] == 0: v = i ** 2 break k = v while True: mode = 0 for i in makediv(k): j = k // i x = bisect.bisect_left(a2, i) - 1 y = bisect.bisect_left(b2, j) - 1 if x < 0 or y < 0: continue if not a[x] ** 2 <= i < (a[x] + 1) ** 2: continue if not b[y] ** 2 <= j < (b[y] + 1) ** 2: continue mode = 1 break if not mode: break k += 1 print(k)