from math import gcd def isprime(n): if n <= 1: return False elif n == 2: return True elif n % 2 == 0: return False A = [2, 325, 9375, 28178, 450775, 9780504, 1795265022] s = 0 d = n - 1 while d % 2 == 0: s += 1 d >>= 1 for a in A: if a % n == 0: return True x = pow(a, d, n) if x != 1: for t in range(s): if x == n - 1: break x = x * x % n else: return False return True def pollard(n): if n % 2 == 0: return 2 if isprime(n): return n f = lambda x: (x * x + 1) % n step = 0 while 1: step += 1 x = step y = f(x) while 1: p = gcd(y - x + n, n) if p == 0 or p == n: break if p != 1: return p x = f(x) y = f(f(y)) def primefact(n): if n == 1: return [] p = pollard(n) if p == n: return [p] left = primefact(p) right = primefact(n // p) left += right return sorted(left) def primedict(n): P = primefact(n) ret = {} for p in P: ret[p] = ret.get(p, 0) + 1 return ret def divisor_lst(n): if n == 1: return [1] primes = primefact(n) primes.append(primes[-1] + 1) bef = primes[0] cnt = 0 ret = [1] for p in primes: if p == bef: cnt += 1 else: times = bef le = len(ret) for _ in range(cnt): for i in range(le): ret.append(ret[i] * times) times *= bef bef = p cnt = 1 ret.sort() return ret n, m = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) ma = max(*A, *B) ca = [False] * (ma + 2) cb = [False] * (ma + 2) for a in A: ca[a] = True for b in B: cb[b] = True if not ca[1] or not cb[1]: print(1) exit() for i in range(1, ma + 2): if ca[i] or cb[i]: continue x = i * i while 1: divs = divisor_lst(x) ok = False for d in divs: a = int(d**0.5) while a * a < d: a += 1 while a * a > d: a -= 1 b = int((x // d) ** 0.5) while b * b < x // d: b += 1 while b * b > x // d: b -= 1 if (ca[a] and cb[b]) or (ca[b] and cb[a]): ok = True break if ok: x += 1 continue else: print(x) exit()