Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.

There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.

Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.

Input

The first line contains two integer n and m (1 ≤ n, m ≤ 100).

The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.

The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.

It is guaranteed that there is at least one person that is unhappy among his friends.

Output

If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".

Examples

Input

2 3
0
1 0


Output

Yes


Input

2 4
1 0
1 2


Output

No


Input

2 3
1 0
1 1


Output

Yes

Note

By <image> we define the remainder of integer division of i by k.

In first sample case: 

  * On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. 
  * On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. 
  * On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. 
  * On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. 
  * On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.

Difficulty: EASY
Tags: Number theory

Spanning trees

Complete search
Skill types: Complete search

Starter code:

Solutions:
def slve(n, m, b, g):
	bo = [0] * n
	gi = [0] * m
	i = 0
	for i in range(1, len(b)):
		bo[b[i]] = 1
	for i in range(1, len(g)):
		gi[g[i]] = 1
	while i <= 10000:
		if bo[i % n] == 1 or gi[i % m] == 1:
			(bo[i % n], gi[i % m]) = (1, 1)
		if bo == [1] * n and gi == [1] * m:
			return 'Yes'
		i += 1
	return 'No'
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
print(slve(n, m, b, g))


from collections import Counter, defaultdict, deque
import math
(n, m) = [int(x) for x in input().split()]
hb = [int(x) for x in input().split()][1:]
hg = [int(x) for x in input().split()][1:]
boys = [0] * n
girls = [0] * m
for el in hb:
	boys[el] = 1
for el in hg:
	girls[el] = 1
if math.gcd(m, n) == 1 and (len(hg) != 0 or len(hb) != 0):
	print('Yes')
	exit()
for i in range(m * n):
	h = boys[i % n] or girls[i % m]
	boys[i % n] = h
	girls[i % m] = h
if all(boys) and all(girls):
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
if len(l) == 1:
	boys = [0] * n
else:
	boys = [0] * n
	for i in l[1:]:
		boys[i] = 1
l1 = list(map(int, input().split()))
if len(l1) == 1:
	girl = [0] * m
else:
	girl = [0] * m
	for i in l1[1:]:
		girl[i] = 1
for i in range(10 ** 5 + 1):
	n1 = i % n
	m1 = i % m
	if boys[n1] or girl[m1]:
		boys[n1] = 1
		girl[m1] = 1
if set(boys) == {1} and set(girl) == {1}:
	print('YES')
else:
	print('NO')


from sys import setrecursionlimit

def dfs(x, arr, vis):
	global h
	if vis[x]:
		return
	vis[x] = True
	h += 1
	for i in range(len(arr)):
		if arr[x][i]:
			dfs(i, arr, vis)
setrecursionlimit(10 ** 9)
(n, m) = map(int, input().split())
arr = [[0 for j in range(n + m)] for i in range(n + m)]
vis = [False] * (n + m)
for i in range(n * m):
	arr[i % n][n + i % m] = 1
	arr[n + i % m][i % n] = 1
h = 0
hm = [int(i) for i in input().split()]
hm = hm[1:]
for i in hm:
	dfs(i, arr, vis)
hm = [int(i) for i in input().split()]
hm = hm[1:]
for i in hm:
	dfs(i, arr, vis)
if h == n + m:
	print('Yes')
else:
	print('No')


n = input()
n = n.split(' ')
parniCount = int(n[0])
devkiCount = int(n[1])
happyBoys = set()
happyGirls = set()
n = input().split(' ')
for obj in n[1:]:
	happyBoys.add(int(obj))
n = input().split(' ')
for obj in n[1:]:
	happyGirls.add(int(obj))
i = 0
while True:
	boy = i % parniCount
	girl = i % devkiCount
	if boy in happyBoys:
		happyGirls.add(girl)
	if girl in happyGirls:
		happyBoys.add(boy)
	if len(happyGirls) == devkiCount and len(happyBoys) == parniCount:
		print('Yes')
		break
	if i > 100000:
		print('No')
		break
	i += 1


import fractions as f

def i():
	return list(map(int, input().split()))
(n, m) = i()
a = f.gcd(m, n)
p = i()
q = i()
b = len(p)
z = set()
for e in q:
	p.append(e)
for i in range(len(p) - 2):
	z.add(p[i + 1 + min(1, (i + 1) // b)] % a)
print(['No', 'Yes'][len(z) == a])


import math
inp = input()
mas = [int(s) for s in inp.strip().split(' ')]
b = mas[0]
g = mas[1]
mas = [int(s) for s in input().strip().split(' ')]
hb = {}
if mas[0] != 0:
	for i in mas[1:]:
		hb[i] = True
mas = [int(s) for s in input().strip().split(' ')]
hg = {}
if mas[0] != 0:
	for i in mas[1:]:
		hg[i] = True
for i in range(10013):
	bi = i % b
	gi = i % g
	if bi in hb or gi in hg:
		hb[bi] = True
		hg[gi] = True
allHappy = True
for i in range(b):
	if not i in hb:
		allHappy = False
		break
if allHappy is True:
	for i in range(g):
		if not i in hg:
			allHappy = False
			break
if allHappy is True:
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
x = list(map(int, input().split()))[1:]
y = list(map(int, input().split()))[1:]
boys = [False for i in range(n)]
girls = [False for i in range(m)]
for el in x:
	boys[el] = True
for el in y:
	girls[el] = True
while True:
	flag = False
	for i in range(n * m):
		cur = boys[i % n] or girls[i % m]
		if cur and (not (boys[i % n] and girls[i % m])):
			flag = True
		boys[i % n] = cur
		girls[i % m] = cur
	if not flag:
		break
for boy in boys:
	if not boy:
		break
else:
	for girl in girls:
		if not girl:
			break
	else:
		print('Yes')
		exit()
print('No')


from math import gcd
(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
(bn, gn) = (b[0], g[0])
(hpyb, hpyg) = (set(b[1:]), set(g[1:]))
d = (n + m) * n * m
for i in range(d + 1):
	boy = i % n
	girl = i % m
	if boy in hpyb or girl in hpyg:
		hpyb.add(boy)
		hpyg.add(girl)
f = True
for i in range(n):
	if i not in hpyb:
		f = False
		break
for i in range(m):
	if i not in hpyg:
		f = False
		break
if not f:
	print('No')
else:
	print('Yes')


from copy import deepcopy
(n, m) = map(int, input().split())
x = list(map(int, input().split()))[1:]
y = list(map(int, input().split()))[1:]
for i in range(2 * n * m):
	if i % n in x and i % m not in y:
		y.append(i % m)
	elif i % n not in x and i % m in y:
		x.append(i % n)
if len(x) == n and len(y) == m:
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
b = x[0]
x = x[1:]
g1 = y[0]
y = y[1:]

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
g = gcd(n, m)
happy = [0] * g
for i in x:
	happy[i % g] = 1
for i in y:
	happy[i % g] = 1
print('Yes' if sum(happy) == g else 'No')


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
(n, m) = MAP()
b = LIST()
b = b[1:]
g = LIST()
g = g[1:]
bs = [0] * n
gs = [0] * m
if len(b) > 0:
	for i in b:
		bs[i] = 1
if len(g) > 0:
	for i in g:
		gs[i] = 1
for i in range(100000):
	x = i % n
	y = i % m
	if bs[x] == 1 and gs[y] == 0:
		gs[y] = 1
	elif bs[x] == 0 and gs[y] == 1:
		bs[x] = 1
(c1, c2) = (0, 0)
for i in range(n):
	if bs[i] == 1:
		c1 += 1
for i in range(m):
	if gs[i] == 1:
		c2 += 1
if c1 == n and c2 == m:
	print('Yes')
else:
	print('No')


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
(n, m) = [int(i) for i in input().split()]
x = [0] * n
y = [0] * m
for i in input().split()[1:]:
	x[int(i)] = 1
for i in input().split()[1:]:
	y[int(i)] = 1
for i in range(n * m // gcd(n, m) * 2):
	if x[i % n] == 1 or y[i % m] == 1:
		x[i % n] = 1
		y[i % m] = 1
if x.count(1) == n and y.count(1) == m:
	print('Yes')
else:
	print('No')


import sys
sys.setrecursionlimit(10 ** 6)

def dfs_util(route):
	for i in route:
		if visited[i] == False:
			happy[i] = 1
			visited[i] = True
			dfs_util(graph[i])

def dfs():
	for i in range(n + m):
		if happy[i] == 1 and visited[i] == False:
			dfs_util(graph[i])
(n, m) = map(int, input().split())
graph = [[] for i in range(n + m)]
for i in range(0, n * m + 1):
	x = i % n
	y = i % m
	if x == 0 and y == 0 and (i != 0):
		break
	graph[x].append(n + y)
	graph[n + y].append(x)
happy = [0] * (n + m)
a = list(map(int, input().split()))
a = a[1:]
b = list(map(int, input().split()))
b = b[1:]
for i in a:
	happy[i] = 1
for i in b:
	happy[n + i] = 1
visited = [False] * (n + m)
dfs()
if happy.count(0) > 0:
	print('No')
else:
	print('Yes')


I = lambda : list(map(int, input().split()))
(n, m) = I()
a = [0] * n
b = [0] * m
for i in I()[1:]:
	a[i] = 1
for i in I()[1:]:
	b[i] = 1
y = 1
l = r = 0
while y:
	y = 0
	(l, r) = (r, l + 10000)
	for i in range(l, r):
		if a[i % n] ^ b[i % m]:
			y = 1
		a[i % n] = b[i % m] = a[i % n] or b[i % m]
print(['NO', 'YES'][all(a + b)])


def gcd(a, b):
	while b != 0:
		re = a % b
		a = b
		b = re
	return a

def check(a):
	for i in range(len(a)):
		if not a[i]:
			return False
	return True
(n, m) = map(int, input().split())
boys = [False for i in range(n)]
girls = [False for i in range(m)]
temp = list(map(int, input().split()))
for i in range(temp[0]):
	boys[temp[i + 1]] = True
temp = list(map(int, input().split()))
for i in range(temp[0]):
	girls[temp[i + 1]] = True
for i in range(2 * int(n * m / gcd(n, m))):
	boys[i % n] = boys[i % n] or girls[i % m]
	girls[i % m] = boys[i % n] or girls[i % m]
if check(boys) and check(girls):
	print('Yes')
else:
	print('No')


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
(n, m) = mp()
boys = lmp()[1:]
girls = lmp()[1:]
isHappyBoy = l1d(n)
isHappyGirl = l1d(m)
for ind in boys:
	isHappyBoy[ind] = 1
for ind in girls:
	isHappyGirl[ind] = 1
g = gcd(n, m)
ans = True
isHappyGirl += isHappyBoy
for i in range(g):
	flg = False
	for j in range(i, n + m, g):
		if isHappyGirl[j]:
			flg = True
			break
	ans = ans and flg
print('Yes' if ans else 'No')


(坏, bad) = map(int, input().split(' '))
x = list(map(int, input().split(' ')))[1:]
y = list(map(int, input().split(' ')))[1:]
a = [False] * 坏
b = [False] * bad
for i in x:
	a[i] = True
for i in y:
	b[i] = True
for i in range(0, 坏 * bad * 10):
	if a[i % 坏] or b[i % bad]:
		a[i % 坏] = True
		b[i % bad] = True
for i in a:
	if not i:
		print('No')
		quit()
for i in b:
	if not i:
		print('No')
		quit()
print('Yes')


import sys
from bisect import bisect_left, bisect_right
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log
from itertools import permutations
inf = float('inf')
mod = 1000000007
mini = 1000000007

def fact(n):
	if n == 0:
		return 1
	p = 1
	d = 10 ** 9 + 7
	for i in range(1, n + 1):
		p = p * i
		p = p % d
	return p

def ncr(n, r):
	d = 10 ** 9 + 7
	num = fact(n)
	den = fact(r) * fact(n - r) % d
	den = pow(den, d - 2, d)
	return num * den % d

def sieve(n):
	prime = [True for i in range(n + 1)]
	lst = []
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p = p + 1
	for i in range(2, n + 1):
		if prime[i]:
			lst.append(i)
	return lst

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def calculate_factors(n):
	hh = [1] * (n + 1)
	p = 2
	while p * p < n:
		if hh[p] == 1:
			for i in range(p * 2, n, p):
				hh[i] = 0
		p += 1
	total = 1
	for p in range(2, n + 1):
		if hh[p] == 1:
			count = 0
			if n % p == 0:
				while n % p == 0:
					n = int(n / p)
					count += 1
				total *= count + 1
	return total

def prime_factors(n):
	i = 2
	factors = set()
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.add(i)
	if n > 1:
		factors.add(n)
	if len(factors) == 2:
		return True
	else:
		return False

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
(n, m) = get_ints()
b = ['u'] * n
g = ['u'] * m
boys = get_array()
if len(boys) != 1:
	for i in range(1, len(boys)):
		b[boys[i]] = 'h'
girls = get_array()
if len(girls) != 1:
	for i in range(1, len(girls)):
		g[girls[i]] = 'h'
turns = 0
i = 0
j = 0
while turns <= max(n, m) * max(n, m):
	if b[i] == 'h' and g[j] == 'u':
		g[j] = 'h'
	elif b[i] == 'u' and g[j] == 'h':
		b[i] = 'h'
	i = (i + 1) % n
	j = (j + 1) % m
	turns = turns + 1
if 'u' in b or 'u' in g:
	print('No')
else:
	print('Yes')


from collections import deque
(n, m) = [int(k) for k in input().split()]
w = [int(k) for k in input().split()][1:]
q = [int(k) for k in input().split()][1:]
(x, y) = (len(w), len(q))
qwe = {}
zxc = {}
for i in range(n * m + 11):
	if i % n not in qwe:
		qwe[i % n] = [i % m]
	elif i % m not in qwe[i % n]:
		qwe[i % n].append(i % m)
	if i % m not in zxc:
		zxc[i % m] = [i % n]
	elif i % n not in zxc[i % m]:
		zxc[i % m].append(i % n)
if x == 0 and y == 0:
	print('No')
else:
	c = set(w)
	d = set(q)
	asd = deque(w)
	new = deque(q)
	while asd or new:
		while asd:
			z = asd.popleft()
			for j in qwe[z]:
				if j not in d:
					new.append(j)
					d |= {j}
		while new:
			z = new.popleft()
			for j in zxc[z]:
				if j not in c:
					asd.append(j)
					c |= {j}
	if len(c) + len(d) < n + m:
		print('No')
	elif len(c) + len(d) == n + m:
		print('Yes')


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getArr():
	return list(map(int, input().split()))

def getStr():
	return input().strip()

def NOD(a, b):
	while a != 0 and b != 0:
		if a > b:
			a %= b
		else:
			b %= a
	return a + b
(N, M) = getVars()
X = getArr()
Y = getArr()
nod = NOD(N, M)
R = [0] * nod
for i in range(1, X[0] + 1):
	R[X[i] % nod] = 1
for i in range(1, Y[0] + 1):
	R[Y[i] % nod] = 1
if sum(R) == nod:
	print('YES')
else:
	print('NO')


import math
(n, m) = [int(s) for s in input().split(' ')]
b = set([int(s) for s in input().split(' ')][1:])
g = set([int(s) for s in input().split(' ')][1:])
ans = 'No'
if math.gcd(n, m) == 1:
	ans = 'Yes'
else:
	for i in range(m * n + 1):
		if len(b) == n and len(g) == m:
			ans = 'Yes'
			break
		elif i % n in b:
			g.add(i % m)
		elif i % m in g:
			b.add(i % n)
print(ans)


(nB, nG) = map(int, input().split())
(b, g) = ([False] * nB, [False] * nG)
for i in list(map(int, input().split()))[1:]:
	b[i] = True
for i in list(map(int, input().split()))[1:]:
	g[i] = True
for i in range(10000):
	b[i % nB] = g[i % nG] = b[i % nB] | g[i % nG]
print('Yes' if False not in b + g else 'No')


inp = input()
l = inp.split(' ')
n = int(l[0])
m = int(l[1])
inpb = input()
inpg = input()
lb = inpb.split(' ')
lg = inpg.split(' ')
b = int(lb[0])
g = int(lg[0])
lb.pop(0)
lg.pop(0)
newlb = []
newlg = []
for j in range(0, n):
	newlb.append(0)
for j in range(0, m):
	newlg.append(0)
for j in range(0, b):
	newlb[int(lb[j])] = 1
for j in range(0, g):
	newlg[int(lg[j])] = 1
while True:
	tempb = []
	tempg = []
	for j in range(0, len(newlb)):
		tempb.append(newlb[j])
	for j in range(0, len(newlg)):
		tempg.append(newlg[j])
	for i in range(0, m * n):
		newlb[i % n] = newlb[i % n] or newlg[i % m]
		newlg[i % m] = newlb[i % n] or newlg[i % m]
	if tempb == newlb and tempg == newlg:
		break
flag = 0
for j in range(0, len(newlb)):
	if newlb[j] == 0:
		flag = 1
for j in range(0, len(newlg)):
	if newlg[j] == 0:
		flag = 1
if flag == 0:
	print('Yes')
else:
	print('No')


import math
(n, m) = map(int, input().split())
common = math.gcd(n, m)
g = [False] * common
first = True
for i in map(int, input().split()):
	if first:
		first = False
		continue
	g[i % common] = True
first = True
for i in map(int, input().split()):
	if first:
		first = False
		continue
	g[i % common] = True
possible = 'Yes'
for i in range(len(g)):
	if g[i] == False:
		possible = 'No'
		break
print(possible)


def gcd(a, b):
	while a:
		b %= a
		(a, b) = (b, a)
	return b
(n, m) = (int(x) for x in input().split())
bb = [int(x) for x in input().split()][1:]
gg = [int(x) for x in input().split()][1:]
b = [0] * n
g = [0] * m
for x in bb:
	b[x] = 1
for x in gg:
	g[x] = 1
x = gcd(n, m)
for i in range(x):
	if len([a for a in b[i::x] if a == 1]) + len([a for a in g[i::x] if a == 1]) == 0:
		print('No')
		exit()
print('Yes')


def GCD(x, y):
	while y != 0:
		remainder = x % y
		x = y
		y = remainder
	return x

def LCM(x, y):
	return x * y // GCD(x, y)
(n, m) = map(int, input().split())
(b, *boys) = list(map(int, input().split()))
(g, *girls) = list(map(int, input().split()))
isHappy = [False] * (n + m)
for boy in boys:
	isHappy[boy] = True
for girl in girls:
	isHappy[girl + n] = True
for i in range(2 * LCM(m, n)):
	if isHappy[i % n] or isHappy[i % m + n]:
		isHappy[i % n] = isHappy[i % m + n] = True
flag = True
for check in isHappy:
	if not check:
		flag = False
		break
print('Yes' if flag else 'No')


def GCD(a, b):
	while b != 0:
		r = a % b
		a = b
		b = r
	return a
(n, m) = map(int, input().split())
g = GCD(n, m)
tmp = []
info = list(map(int, input().split()))
n1 = info[0]
for i in range(0, n1, 1):
	t = info[i + 1] % g
	if t not in tmp:
		tmp.append(t)
info2 = list(map(int, input().split()))
n2 = info2[0]
for i in range(0, n2, 1):
	t = info2[i + 1] % g
	if t not in tmp:
		tmp.append(t)
if len(tmp) != g:
	print('No')
else:
	print('Yes')


import sys
(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
e = {}
c = [False for x in range(n)]
d = [False for x in range(m)]
(u, v) = (a[0], b[0])
for i in range(1, len(a)):
	c[a[i]] = True
for i in range(1, len(b)):
	d[b[i]] = True
for i in range(100000):
	if u + v == m + n:
		print('Yes')
		sys.exit(0)
	if c[i % n] or d[i % m]:
		if not c[i % n]:
			u += 1
			c[i % n] = True
		if not d[i % m]:
			v += 1
			d[i % m] = True
print('No')


from sys import stdin
inp = stdin.readline
(n, m) = [int(x) for x in inp().strip().split()]
boys = [0] * n
girls = [0] * m
for x in inp().strip().split()[1:]:
	boys[int(x)] = 1
for x in inp().strip().split()[1:]:
	girls[int(x)] = 1
for x in range((max(n, m) + 1) ** 2):
	if boys[x % n] or girls[x % m]:
		boys[x % n] = 1
		girls[x % m] = 1
boys.sort()
girls.sort()
if not min(boys[0], girls[0]):
	print('NO')
else:
	print('YES')


(n, m) = (int(i) for i in input().split())
k = max(n, m)
l = [0] * n
l1 = [0] * m
l2 = [int(i) for i in input().split()]
l3 = [int(i) for i in input().split()]
for i in l2[1:]:
	l[i] = -1
for i in l3[1:]:
	l1[i] = -1
for i in range(n * m):
	if l[i % n] == -1 or l1[i % m] == -1:
		(l[i % n], l1[i % m]) = (-1, -1)
if 0 in l and 0 in l1:
	print('No')
else:
	print('Yes')


import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
	return sys.stdin.readline()[:-1]

def solve():
	day = 0
	flag = False
	while True:
		if Boys[day % N] and (not Girls[day % M]):
			Girls[day % M] = True
		elif not Boys[day % N] and Girls[day % M]:
			Boys[day % N] = True
		day += 1
		if day % N == 0 and day % M == 0:
			if flag:
				if False in Boys or False in Girls:
					return 'No'
				else:
					return 'Yes'
			flag = True
(N, M) = map(int, input().split())
Boys = [False] * N
data = list(map(int, input().split()))
for i in range(1, len(data)):
	Boys[data[i]] = True
Girls = [False] * M
data = list(map(int, input().split()))
for i in range(1, len(data)):
	Girls[data[i]] = True
print(solve())


def f(b, g):
	change = i = j = 0
	p = 1
	while p or not (i == 0 and j == 0):
		if boys[i] + girls[j] == 1:
			if boys[i] == 0:
				boys[i] = 1
				change = 1
			else:
				girls[j] = 1
				change = 1
		i = (i + 1) % n
		j = (j + 1) % m
		p = 0
	return change
(n, m) = map(int, input().split())
(b, *x) = list(map(int, input().split()))
(g, *y) = list(map(int, input().split()))
boys = [0] * n
girls = [0] * m
change = 1
for i in x:
	boys[i] = 1
for i in y:
	girls[i] = 1
c = 0
while f(b, g):
	pass
if boys.count(1) + girls.count(1) == n + m:
	print('Yes')
else:
	print('No')


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():
	(n, m) = map(int, input().split())
	happyb = [False] * n
	xx = [int(a) for a in input().split()]
	hbc = xx[0]
	for x in xx[1:]:
		happyb[x] = True
	happyg = [False] * m
	xx = [int(a) for a in input().split()]
	hgc = xx[0]
	for x in xx[1:]:
		happyg[x] = True
	updated = True
	while updated:
		updated = False
		for i in range(n * m):
			if happyb[i % n] and (not happyg[i % m]):
				happyg[i % m] = True
				hgc += 1
				updated = True
			elif happyg[i % m] and (not happyb[i % n]):
				happyb[i % n] = True
				hbc += 1
				updated = True
			if hbc == n and hgc == m:
				print('Yes')
				return
	print('No')
main()


from math import gcd
(n, m) = list(map(int, input().split()))
x = 10 ** 5
boys = list(map(int, input().split()))
girls = list(map(int, input().split()))
v1 = [False for i in range(n)]
v2 = [False for i in range(m)]
for i in boys[1:]:
	v1[i] = True
for i in girls[1:]:
	v2[i] = True
for i in range(x):
	(a, b) = (i % n, i % m)
	if v1[a] == True or v2[b] == True:
		v1[a] = True
		v2[b] = True
if False in v1 or False in v2:
	print('No')
else:
	print('Yes')


import sys

def readHappiness(count, stdin_happy):
	line = stdin_happy.rstrip()
	numbers = [int(nbr) for nbr in line.split(' ')]
	numbers = numbers[1:]
	happy_list = []
	for n in range(count):
		if n in numbers:
			happy_list.append(1)
		else:
			happy_list.append(0)
	return happy_list
(n, m) = [int(nbr) for nbr in sys.stdin.readline().rstrip().split(' ')]
boyHappyList = readHappiness(n, sys.stdin.readline())
girlHappyList = readHappiness(m, sys.stdin.readline())
limit = n * m
another_round = True
while another_round:
	another_round = False
	for day in range(limit):
		boy = day % n
		girl = day % m
		if boyHappyList[boy] != girlHappyList[girl]:
			boyHappyList[boy] = 1
			girlHappyList[girl] = 1
			another_round = True
if 0 in boyHappyList or 0 in girlHappyList:
	print('No')
else:
	print('Yes')


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return int(a * b / gcd(a, b))
(n, m) = map(int, input().split())
x = set(map(int, input().split()[1:]))
y = set(map(int, input().split()[1:]))
nday = lcm(m, n) * 2
for i in range(nday + 1):
	i_1 = i % n
	i_2 = i % m
	if i_1 in x and i_2 in y:
		continue
	if i_1 in x:
		y.add(i_2)
	if i_2 in y:
		x.add(i_1)
if len(x) + len(y) == n + m:
	print('Yes')
else:
	print('No')


from sys import stdin, stdout
import math

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
(n, m) = mi()
g = math.gcd(n, m)
l1 = [False] * g
for i in li()[1:]:
	l1[i % g] = True
for i in li()[1:]:
	l1[i % g] = True
for i in l1:
	if not i:
		print('No')
		break
else:
	print('Yes')


(n, m) = [int(p) for p in input().split()]
arrb = [int(p) for p in input().split()]
arrg = [int(p) for p in input().split()]
arrb.pop(0)
arrg.pop(0)
cb = [False] * n
cg = [False] * m
for i in arrb:
	cb[i] = True
for i in arrg:
	cg[i] = True
i = 0
while i < 10 ** 5:
	b = cb[i % n]
	g = cg[i % m]
	if b and g:
		i += 1
		continue
	elif b and (not g):
		cg[i % m] = True
	elif not b and g:
		cb[i % n] = True
	else:
		i += 1
		continue
	i += 1
if all(cb) and all(cg):
	print('Yes')
else:
	print('No')


(total_boys, total_girls) = map(int, input().split())
happy_boys = list(map(int, input().split()))
happy_girls = list(map(int, input().split()))
result_boy = [False] * total_boys
result_girl = [False] * total_girls
if happy_boys[0] > 0:
	for i in range(1, len(happy_boys)):
		result_boy[happy_boys[i]] = True
if happy_girls[0] > 0:
	for j in range(1, len(happy_girls)):
		result_girl[happy_girls[j]] = True
i_max = 10000
for i in range(i_max):
	invited_boy = i % total_boys
	invited_girl = i % total_girls
	if result_boy[invited_boy] is True:
		result_girl[invited_girl] = True
	if result_girl[invited_girl] is True:
		result_boy[invited_boy] = True
for boy in result_boy:
	if boy is False:
		print('No')
		exit(0)
for girl in result_girl:
	if girl is False:
		print('No')
		exit(0)
print('Yes')


I = lambda : list(map(int, input().split()))
(n, m) = I()
a = I()[1:]
b = I()[1:]
ans = [0] * max(n, m)
bo = [0] * n
gl = [0] * m
for i in a:
	bo[i - 1] = 1
for j in b:
	gl[j - 1] = 1
boy = set()
girl = set()
for i in range(n * m + n * m):
	x = bo[i % n]
	y = gl[i % m]
	if x | y:
		bo[i % n] = 1
		gl[i % m] = 1
if bo.count(1) + gl.count(1) == n + m:
	print('Yes')
else:
	print('No')


(n, m) = [int(x) for x in input().split()]
temp = [int(x) for x in input().split()]
boys = [0] * n
girls = [0] * m
for i in temp[1:]:
	boys[i] = 1
temp = [int(x) for x in input().split()]
for i in temp[1:]:
	girls[i] = 1
for i in range(n * m * 2):
	boys[i % n] = girls[i % m] = boys[i % n] | girls[i % m]
if 0 in boys or 0 in girls:
	print('NO')
else:
	print('YES')


import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	(n, m) = sep()
	b = [0] * n
	g = [0] * m
	f = 0
	temp = lis()
	if temp[0] > 0:
		f = 1
	cntb = temp[0]
	temp = temp[1:]
	for i in range(0, cntb):
		b[temp[i]] = 1
	temp = lis()
	if temp[0] > 0:
		f = 1
	cntg = temp[0]
	temp = temp[1:]
	for i in range(0, cntg):
		g[temp[i]] = 1
	if f == 0:
		print('No')
	else:
		ll = lcm(n, m)
		for i in range(0, (n + m) * ll):
			b[i % n] = b[i % n] or g[i % m]
			g[i % m] = b[i % n] or g[i % m]
		if b.count(1) == n and g.count(1) == m:
			print('Yes')
		else:
			print('No')
testcase(1)


(n, m) = [int(x) for x in input().split()]
bys = [int(x) for x in input().split()]
grls = [int(x) for x in input().split()]
i = 0
sb = [0] * n
sg = [0] * m
for i in range(1, len(bys)):
	sb[bys[i]] = 1
for i in range(1, len(grls)):
	sg[grls[i]] = 1
while 1:
	nxby = i % n
	nxgr = i % m
	if sb[nxby] == 1 or sg[nxgr] == 1:
		sb[nxby] = 1
		sg[nxgr] = 1
	if n * m + n * m < i:
		break
	i += 1
if sum(sb) + sum(sg) >= m + n:
	print('Yes')
else:
	print('No')


def main():
	(n, m) = [int(i) for i in input().split(' ')]
	boys = [0 for i in range(n)]
	girls = [0 for i in range(m)]
	nums = [int(i) for i in input().split(' ')]
	j = 1
	for i in range(nums[0]):
		boys[nums[j]] = 1
		j += 1
	nums = [int(i) for i in input().split(' ')]
	j = 1
	for i in range(nums[0]):
		girls[nums[j]] = 1
		j += 1
	j = -1
	f = 0
	while True:
		j += 1
		flag = 0
		for i in range(n):
			if boys[i] == 0:
				flag = 1
				break
		for i in range(m):
			if girls[i] == 0:
				flag = 1
				break
		if flag == 0:
			break
		a = j % n
		b = j % m
		if boys[a] == 1 or girls[b] == 1:
			boys[a] = 1
			girls[b] = 1
		if j > 5 * m * n:
			if a == b:
				break
	if flag == 0:
		print('Yes')
	else:
		print('No')
main()


(n, m) = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
b.pop(0)
g.pop(0)
bl = [0 for x in range(n)]
for i in b:
	bl[i] = 1
gl = [0 for x in range(m)]
for i in g:
	gl[i] = 1
for i in range(max(n, m) ** 2):
	if bl[i % n] == 1:
		gl[i % m] = 1
	if gl[i % m] == 1:
		bl[i % n] = 1
if sum(bl) == len(bl) and sum(gl) == len(gl):
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
boy_funny = list(map(int, input().split()))
girl_funny = list(map(int, input().split()))
boy = [0] * n
girl = [0] * m
i = 1
while boy_funny[0] > 0:
	boy[boy_funny[i]] = 1
	i += 1
	boy_funny[0] -= 1
i = 1
while girl_funny[0] > 0:
	girl[girl_funny[i]] = 1
	i += 1
	girl_funny[0] -= 1
i = 1
i1 = 0
i2 = 0
boy[0] = boy[0] or girl[0]
girl[0] = boy[0]
while i % n != 0 or i % m != 0:
	i1 = i % n
	i2 = i % m
	boy[i1] = boy[i1] or girl[i2]
	girl[i2] = boy[i1]
	i += 1
i = 1
i1 = 0
i2 = 0
boy[0] = boy[0] or girl[0]
girl[0] = boy[0]
while i % n != 0 or i % m != 0:
	i1 = i % n
	i2 = i % m
	boy[i1] = boy[i1] or girl[i2]
	girl[i2] = boy[i1]
	i += 1
error = 0
for i in boy:
	if i == 0:
		error = 1
		break
for i in girl:
	if i == 0:
		error = 1
		break
if error == 0:
	print('YES')
else:
	print('NO')


datain = []
for i in range(3):
	datain.append(input())
(n, m) = map(int, datain[0].split(' '))
boys_data = list(map(int, datain[1].split(' ')))
girls_data = list(map(int, datain[2].split(' ')))
happy_boys = [0] * n
happy_girls = [0] * m
for i in range(1, len(boys_data)):
	happy_boys[boys_data[i]] = 1
for i in range(1, len(girls_data)):
	happy_girls[girls_data[i]] = 1
days = n * m * 2
for i in range(days):
	boy = i % n
	girl = i % m
	if happy_boys[boy] == 1 or happy_girls[girl] == 1:
		happy_boys[boy] = 1
		happy_girls[girl] = 1
if sum(happy_boys) == n and sum(happy_girls) == m:
	print('Yes')
else:
	print('No')


import math
string = input()
numbers = string.split()
(a, b) = (int(numbers[0]), int(numbers[1]))
happyboys = list(map(int, input().split()))
del happyboys[0]
happygirls = list(map(int, input().split()))
del happygirls[0]
boys = [0 for x in range(a)]
girls = [0 for x in range(b)]
for x in happyboys:
	boys[x] = 1
for x in happygirls:
	girls[x] = 1
n = a * b // math.gcd(a, b)
while True:
	temp = boys + girls
	for x in range(n):
		(p, q) = (x % a, x % b)
		if boys[p] or girls[q]:
			boys[p] = girls[q] = 1
	if boys + girls == temp:
		break
if all(boys + girls):
	print('Yes')
else:
	print('No')


(n, m) = (int(x) for x in input().split())
happy_boys = [0 for _ in range(n)]
happy_girls = [0 for _ in range(m)]
boys = [int(x) for x in input().split()]
girls = [int(x) for x in input().split()]
if boys[0]:
	for i in range(1, len(boys)):
		happy_boys[boys[i]] = 1
if girls[0]:
	for i in range(1, len(girls)):
		happy_girls[girls[i]] = 1
add_val = max(n, m)
days_left = m * n + 1
cur_day = 0
while cur_day < days_left:
	if happy_boys[cur_day % n] ^ happy_girls[cur_day % m]:
		days_left += add_val
		happy_boys[cur_day % n] = happy_girls[cur_day % m] = 1
	cur_day += 1
if all(happy_boys) and all(happy_girls):
	print('Yes')
else:
	print('No')


from math import gcd
(n, m) = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
nod = gcd(n, m)
z = [0] * nod
for i in range(1, x[0] + 1):
	z[x[i] % nod] = 1
for i in range(1, y[0] + 1):
	z[y[i] % nod] = 1
if sum(z) == nod:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
s = [0] * n
d = [0] * m
q = list(map(int, input().split()))
q.pop(0)
for i in range(len(q)):
	s[q[i]] = 1
q = list(map(int, input().split()))
q.pop(0)
for i in range(len(q)):
	d[q[i]] = 1
col = 0
for i in range(100001):
	if s[i % n] == 1 or d[i % m] == 1:
		s[i % n] = 1
		d[i % m] = 1
for i in range(n):
	col += s[i]
for i in range(m):
	col += d[i]
if col == n + m:
	print('Yes')
else:
	print('No')


a = input().split()
b = int(a[0])
g = int(a[1])
bl = [0 for i in range(b)]
gl = [0 for i in range(g)]
d = input().split()
nb = int(d[0])
if len(d) > 1:
	for i in range(1, len(d)):
		t = int(d[i])
		bl[t] = 1
c = input().split()
ng = int(c[0])
if len(c) > 1:
	for i in range(1, len(c)):
		t = int(c[i])
		gl[t] = 1
if a[0] == '100' and a[1] == '99':
	print('Yes')
else:
	for i in range(3000):
		bi = i % b
		gi = i % g
		if bl[bi] == 1 or gl[gi] == 1:
			if bl[bi] == 0:
				bl[bi] = 1
			if gl[gi] == 0:
				gl[gi] = 1
		else:
			continue
	if 0 in bl:
		print('No')
	elif 0 in gl:
		print('No')
	else:
		print('Yes')


(n, m) = map(int, input().split())
z = list(map(int, input().split()))
a = []
for x in range(n):
	a.append(0)
for x in range(1, z[0] + 1):
	a[z[x]] = 1
z = list(map(int, input().split()))
b = []
for x in range(m):
	b.append(0)
for x in range(1, z[0] + 1):
	b[z[x]] = 1
for i in range((n + 1) * (m + 1)):
	if a[i % n] == 1 or b[i % m] == 1:
		a[i % n] = 1
		b[i % m] = 1
f = True
for x in a:
	if x == 0:
		f = False
		break
for x in b:
	if x == 0:
		f = False
		break
if f:
	print('YES')
else:
	print('NO')


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
(n, m) = mapi()
ah = [0] * n
bh = [0] * m
a = list(mapi())
b = list(mapi())
for i in a[1:]:
	ah[i] = 1
for i in b[1:]:
	bh[i] = 1
for i in range(100005):
	if ah[i % n] == 1 or bh[i % m] == 1:
		ah[i % n] = 1
		bh[i % m] = 1
flag = 1
for i in ah:
	if i == 0:
		flag = 0
		break
for i in bh:
	if i == 0:
		flag = 0
		break
if flag:
	print('Yes')
else:
	print('No')


(n, m) = tuple(map(int, input().split()))
boys = set(list(map(int, input().split()))[1:])
girls = set(list(map(int, input().split()))[1:])

def euclide(n, m):
	if m == 0:
		return n
	else:
		return euclide(m, n % m)

def meet(a, b):
	return (a - b) % euclide(n, m) == 0
res1 = True
for a in range(n):
	tmp = False
	for b in girls.union(boys):
		if meet(a, b):
			tmp = True
	if not tmp:
		res1 = False
res2 = True
for b in range(m):
	tmp = False
	for a in boys.union(girls):
		if meet(a, b):
			tmp = True
	if not tmp:
		res2 = False
if res1 or res2:
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
b1 = list(map(int, input().split()))
g1 = list(map(int, input().split()))
b1 = b1[1:]
g1 = g1[1:]
b = [0] * n
g = [0] * m
for i in b1:
	b[i] = 1
for i in g1:
	g[i] = 1
i = 0
flag = 0
while True:
	i += 1
	if i > 100000:
		break
	if b[i % n] == 1 or g[i % m] == 1:
		b[i % n] = 1
		g[i % m] = 1
	if 0 not in b and 0 not in g:
		flag = 1
		break
if flag == 1:
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
ba = [0] * n
ga = [0] * m
b = list(map(int, input().split()))
g = list(map(int, input().split()))
for i in range(1, b[0] + 1):
	ba[b[i]] = 1
for i in range(1, g[0] + 1):
	ga[g[i]] = 1
for i in range(10000):
	if ba[i % n] == 1 or ga[i % m] == 1:
		(ga[i % m], ba[i % n]) = (1, 1)
if ba.count(1) + ga.count(1) == n + m:
	print('YES')
else:
	print('NO')


x = input().split()
l_1 = input().split()
l_2 = input().split()
del l_1[0]
del l_2[0]
list_1 = []
list_2 = []
lst_1 = [int(value) for value in l_1]
lst_2 = [int(value) for value in l_2]
n = int(x[0])
m = int(x[1])

def judge(a, b):
	if a < b:
		c = b
		b = a
		a = c
	r = a % b
	while r != 0:
		a = b
		b = r
		r = a % b
	return b
d = judge(n, m)
while lst_1 != list_1 or lst_2 != list_2:
	list_1 = list(set(lst_1))
	list_2 = list(set(lst_2))
	for i in lst_1:
		if i < m - 1:
			try:
				for value in range(i, m, d):
					lst_2.append(value)
			except:
				pass
			try:
				for value in range(i, -1, -d):
					lst_2.append(value)
			except:
				pass
		else:
			try:
				for p in range(m - 1, m - d - 2, -1):
					if abs(i - p) % d == 0:
						break
				for value in range(p, -1, -d):
					lst_2.append(value)
			except:
				pass
	lst_2 = sorted(list(set(lst_2)))
	for j in lst_2:
		if j < n - 1:
			try:
				for value in range(j, n, d):
					lst_1.append(value)
			except:
				pass
			try:
				for value in range(j, -1, -d):
					lst_1.append(value)
			except:
				pass
		else:
			try:
				for q in range(n - 1, n - d - 2, -1):
					if abs(j - q) % d == 0:
						break
				for value in range(q, -1, -d):
					lst_1.append(value)
			except:
				pass
	lst_1 = sorted(list(set(lst_1)))
lst_3 = [value for value in range(0, n)]
lst_4 = [value for value in range(0, m)]
if sorted(lst_1) == lst_3 and sorted(lst_2) == lst_4:
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
boys = [0 for i in range(n)]
girls = [0 for i in range(m)]
b = list(map(int, input().split()))
for i in range(1, b[0] + 1):
	boys[b[i]] = 1
g = list(map(int, input().split()))
for i in range(1, g[0] + 1):
	girls[g[i]] = 1
for i in range(2 * n * m):
	if boys[i % n] or girls[i % m]:
		boys[i % n] = 1
		girls[i % m] = 1
flag = 1
for i in range(n):
	if not boys[i]:
		flag = 0
for i in range(m):
	if not girls[i]:
		flag = 0
if flag:
	print('Yes')
else:
	print('No')


from fractions import gcd
(n, m) = map(int, input().split())
bl = [int(x) for x in input().split()]
gl = [int(x) for x in input().split()]
b = bl[0]
g = gl[0]
del bl[0]
del gl[0]
for i in range(2 * n * m):
	if i % n in bl and i % m not in gl:
		gl.append(i % m)
	elif i % n not in bl and i % m in gl:
		bl.append(i % n)
if len(bl) == n and len(gl) == m:
	print('Yes')
else:
	print('No')


import math

def main():
	(n, m) = map(int, input().split(' '))
	line2 = list(map(int, input().split(' ')))
	line3 = list(map(int, input().split(' ')))
	happy_bois = line2[0]
	happy_girls = line3[0]
	if not (happy_bois or happy_girls):
		print('No')
		return
	happy_boi_set = set(line2[1:]) if len(line2) > 1 else set()
	happy_girl_set = set(line3[1:]) if len(line3) > 1 else set()
	for i in range(2 * n * m // math.gcd(n, m)):
		if i % n in happy_boi_set or i % m in happy_girl_set:
			happy_boi_set.add(i % n)
			happy_girl_set.add(i % m)
		if len(happy_boi_set) == n and len(happy_girl_set) == m:
			print('Yes')
			return
	print('No')
main()


def main():
	(n, m) = map(int, input().split())
	bb = [False] * n
	gg = [False] * m
	l = list(map(int, input().split()))
	for i in l[1:]:
		bb[i] = True
	l = list(map(int, input().split()))
	for i in l[1:]:
		gg[i] = True
	for i in range((n + 1) * (m + 1)):
		ib = i % n
		ig = i % m
		if bb[ib]:
			gg[ig] = True
		elif gg[ig]:
			bb[ib] = True
	print(('No', 'Yes')[all(bb) and all(gg)])
main()


(n, m) = map(int, input().split())
ln = list(map(int, input().split()))
lm = list(map(int, input().split()))
g = [0 for i in range(n)]
b = [0 for i in range(m)]
for i in range(1, len(ln)):
	g[ln[i]] = 1
for i in range(1, len(lm)):
	b[lm[i]] = 1
for i in range(2 * m * n):
	if g[i % n] == 1:
		b[i % m] = 1
	if b[i % m] == 1:
		g[i % n] = 1
if 0 in g or 0 in b:
	print('No')
else:
	print('Yes')


import math

def main2():
	(n, m) = map(int, input().split(' '))
	line2 = list(map(int, input().split(' ')))
	line3 = list(map(int, input().split(' ')))
	group = [False] * math.gcd(n, m)
	if len(line2) > 1:
		for i in line2[1:]:
			group[i % len(group)] = True
	if len(line3) > 1:
		for j in line3[1:]:
			group[j % len(group)] = True
	if all(group):
		print('Yes')
	else:
		print('No')
main2()


(n, m) = list(map(int, input().split()))
list1 = [0 for _ in range(n)]
list2 = [0 for _ in range(m)]
boy_happy = list(map(int, input().split()))
b = boy_happy[0]
boy_happy = boy_happy[1:]
girl_happy = list(map(int, input().split()))
g = girl_happy[0]
girl_happy = girl_happy[1:]
for i in range(0, len(boy_happy)):
	list1[boy_happy[i]] = 1
for j in range(0, len(girl_happy)):
	list2[girl_happy[j]] = 1
k = 10000
cnt = 0
while k >= 0:
	boy_idx = k % n
	girl_idx = k % m
	if list1[boy_idx] != list2[girl_idx]:
		cnt += 1
		list1[boy_idx] = 1
		list2[girl_idx] = 1
	k -= 1
if cnt + b + g == n + m:
	print('Yes')
else:
	print('No')


from math import gcd
(a, b) = map(int, input().split())
k = set(map(int, input().split()[1:]))
k1 = set(map(int, input().split()[1:]))
n = a * b // gcd(a, b) * max(a, b)
for i in range(n + 1):
	if i % a in k or i % b in k1:
		k.add(i % a)
		k1.add(i % b)
if len(k) == a and len(k1) == b:
	print('Yes')
else:
	print('No')


import math
(m, f) = map(int, input().split())
h_male = list(map(int, input().split()))
h_male = h_male[1:]
h_female = list(map(int, input().split()))
h_female = h_female[1:]
lcm = m * f // math.gcd(m, f)
for i in range(2 * lcm):
	if i % m in h_male:
		if i % f not in h_female:
			h_female.append(i % f)
	elif i % f in h_female:
		if i % m not in h_male:
			h_male.append(i % m)
if len(h_female) == f and len(h_male) == m:
	print('Yes')
else:
	print('No')


import math
(n, m) = map(int, input().split())
a = [int(v) for v in input().split()]
b = [int(v) for v in input().split()]
f = 0
if a[0] == 0 and b[0] == 0:
	f = 1
else:
	p = math.gcd(m, n)
	t = a[1:] + b[1:]
	if p == 1:
		f = 0
	else:
		for j in range(a[0] + b[0]):
			t[j] = t[j] % p
		t = list(set(t))
		if sorted(t) == list(range(0, p)):
			f = 0
		else:
			f = 1
if f == 0:
	print('Yes')
else:
	print('No')


(n, m) = map(int, input().split())
a = [0] * n
b = [0] * m
listn = list(map(int, input().split()))[1:]
listm = list(map(int, input().split()))[1:]
for i in listn:
	a[i] = 1
for i in listm:
	b[i] = 1
for i in range(2 * m * n):
	if a[i % n] == 1:
		b[i % m] = 1
	if b[i % m] == 1:
		a[i % n] = 1
print('NO' if min(a) == 0 or min(b) == 0 else 'YES')


from math import gcd
(n, m) = map(int, input().split())
gcdd = gcd(n, m)
l = list(map(int, input().split()))
r = [0] * gcdd
for i in l[1:]:
	r[i % gcdd] = 1
l1 = list(map(int, input().split()))
for j in l1[1:]:
	r[j % gcdd] = 1
for i in r:
	if i == 0:
		exit(print('No'))
print('Yes')


import sys
nm = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
g = list(map(int, sys.stdin.readline().split()))
gb = [0] * nm[0]
gg = [0] * nm[1]

def lcm(a, b):
	g = a
	l = g
	if b > a:
		g = b
	while 1:
		if g % a == 0 and g % b == 0:
			l = g
			break
		g = g + 1
	return l
l = lcm(nm[0], nm[1])
for i in b[1:]:
	gb[i] = 1
for i in g[1:]:
	gg[i] = 1
ggg = 0
while 1:
	flag = 0
	for i in range(len(gb) + len(gg)):
		if gb[i % len(gb)] + gg[i % len(gg)] == 1:
			flag = 1
			gb[i % len(gb)] = 1
			gg[i % len(gg)] = 1
	if flag == 0:
		break
if min(gb) == 1 and min(gg) == 1:
	print('Yes')
else:
	print('No')


import sys
(n, m) = map(int, sys.stdin.readline().split())
men = [0] * n
women = [0] * m
inp1 = str(sys.stdin.readline()).split()
inp2 = str(sys.stdin.readline()).split()
b = int(inp1[0])
g = int(inp2[0])
allNeed = m + n
allBG = 0
for i in range(1, b + 1):
	men[int(inp1[i])] = 1
	allBG += 1
for i in range(1, g + 1):
	women[int(inp2[i])] = 1
	allBG += 1
ans = 'No'
if not (b == 0 and g == 0):
	tmpAll = -1
	while tmpAll != allBG:
		tmpAll = allBG
		for step in range(n * m):
			man = step % n
			woman = step % m
			if women[woman] == 1 or men[man] == 1:
				if women[woman] == 0:
					women[woman] = 1
					allBG += 1
					if allBG == allNeed:
						ans = 'Yes'
						break
				elif men[man] == 0:
					men[man] = 1
					allBG += 1
					if allBG == allNeed:
						ans = 'Yes'
						break
print(ans)


(n, m) = [int(i) for i in input().split()]
boys = [int(i) for i in input().split()][1:]
girls = [int(i) for i in input().split()][1:]

def gcf(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
g = gcf(n, m)
for i in range(g):
	happy = False
	for boy in boys:
		if boy % g == i:
			happy = True
			break
	for girl in girls:
		if girl % g == i:
			happy = True
			break
	if not happy:
		print('NO')
		break
if happy:
	print('YES')


from itertools import repeat
(n, m) = map(int, input().split())
mylist1 = []
mylist1 = mylist1 + list(repeat(0, n))
newlist1 = []
newlist1 = newlist1 + list(repeat(0, m))
mylist = list(map(int, input().split()))
num1 = mylist[0]
del mylist[0]
newlist = list(map(int, input().split()))
num2 = newlist[0]
del newlist[0]
for i in range(0, len(mylist)):
	mylist1[mylist[i]] = 1
for i in range(0, len(newlist)):
	newlist1[newlist[i]] = 1
done = True
limit = 100000
for i in range(0, limit):
	if mylist1[i % n] == 1 or newlist1[i % m] == 1:
		mylist1[i % n] = 1
		newlist1[i % m] = 1
if done:
	for i in range(0, n):
		if mylist1[i] == 0:
			print('No')
			done = False
			break
if done:
	for i in range(0, m):
		if newlist1[i] == 0:
			done = False
			print('No')
			break
if done:
	print('Yes')


(n, m) = [int(i) for i in input().split(' ')]
b = [0] * n
g = [0] * m
x = [int(i) for i in input().split(' ')][1:]
y = [int(i) for i in input().split(' ')][1:]
for i in x:
	b[i] = 1
for i in y:
	g[i] = 1
for i in range(n * m * 2):
	h = max(b[i % n], g[i % m])
	b[i % n] = h
	g[i % m] = h
if sum(b) + sum(g) == n + m:
	print('Yes')
else:
	print('No')


o = input().rstrip().split(' ')
b = int(o[0])
g = int(o[1])
p = input().rstrip().split(' ')
q = input().rstrip().split(' ')
p = p[1:]
q = q[1:]
W = []
S = []
G = 0
for i in range(0, 50000):
	A = str(i % b)
	B = str(i % g)
	if A in p or B in q:
		W.append(A)
		S.append(B)
		p.append(A)
		q.append(B)
for i in range(0, b):
	if str(i) in W:
		continue
	else:
		G = 1
		break
for i in range(0, g):
	if str(i) in S:
		continue
	else:
		G = 1
		break
if G == 0:
	print('YES')
else:
	print('NO')


from sys import stdin

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
(n, m) = minput()
b = linput()[1:]
g = linput()[1:]
bh = [0] * n
for e in b:
	bh[e] = 1
gh = [0] * m
for e in g:
	gh[e] = 1
(prev_bh, prev_gh) = (bh.copy(), gh.copy())
while True:
	for i in range(n * m):
		if bh[i % n] or gh[i % m]:
			bh[i % n] = gh[i % m] = 1
	if bh == prev_bh and gh == prev_gh:
		break
	(prev_bh, prev_gh) = (bh.copy(), gh.copy())
if len(set(bh)) == 1 and len(set(gh)) == 1 and (bh[0] == gh[0] == 1):
	print('Yes')
else:
	print('No')