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 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4b8097bd0e14e104f0d3ee4c567d8dae1d841e28.png" style="max-width: 100.0%;max-height: 100.0%;"/>-th boy and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/affc1977a5192853063fd8e722c851c3e5503c85.png" style="max-width: 100.0%;max-height: 100.0%;"/>-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 it is happy originally), he stays happy forever.

Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
The first line contains two integer *n* and *m* (1 ≤ *n*, *m* ≤ 10<sup class="upper-index">9</sup>).

The second line contains integer *b* (0 ≤ *b* ≤ *min*(*n*, 10<sup class="upper-index">5</sup>)), denoting the number of happy boys among friends of Drazil, and then follow *b* distinct integers *x*<sub class="lower-index">1</sub>, *x*<sub class="lower-index">2</sub>, ..., *x*<sub class="lower-index">*b*</sub> (0 ≤ *x*<sub class="lower-index">*i*</sub> &lt; *n*), denoting the list of indices of happy boys.

The third line conatins integer *g* (0 ≤ *g* ≤ *min*(*m*, 10<sup class="upper-index">5</sup>)), denoting the number of happy girls among friends of Drazil, and then follow *g* distinct integers *y*<sub class="lower-index">1</sub>, *y*<sub class="lower-index">2</sub>, ... , *y*<sub class="lower-index">*g*</sub> (0 ≤ *y*<sub class="lower-index">*j*</sub> &lt; *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.
a = input()
a = a.split()
n, m = int(a[0]), int(a[1])
boys = [False for k in range(n)]
girls = [False for k in range(m)]
a = input()
a = a.split()
if len(a) > 1:
    for i in range(int(a[0])):
        boys[int(a[i+1])] = True
a = input()
a = a.split()
if len(a) > 1:
    for i in range(int(a[0])):
        girls[int(a[i+1])] = True

i = 0
while sum(boys)!=len(boys) and sum(girls)!=len(girls):
    if (boys[i%n] and not girls[i%m]) or (not boys[i%n] and girls[i%m]):
        boys[i%n] = True
        girls[i%m] = True
    i += 1
print(i)
    
