#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
void exgcd(long long a, long long b, long long& x, long long& y,
           long long& gcd) {
  if (b == 0) return x = 1, y = 0, gcd = a, void();
  exgcd(b, a % b, x, y, gcd);
  long long t = x;
  x = y;
  y = t - a / b * y;
}
long long n, m, d, x, y, cnt;
vector<long long> g[400005], b[400005];
struct node {
  long long p, dis, id;
  bool operator<(const node& b) const { return p < b.p; }
} a[400005];
long long solve(long long m, long long n, long long x, vector<long long> g,
                vector<long long> b) {
  if (g.size() == m) return -2333;
  cnt = 0;
  long long res = 0, dis = 1e18;
  for (auto u : g) a[++cnt] = (node){u * x % m, u, cnt};
  for (auto u : b) a[++cnt] = (node){u * x % m, u, cnt};
  sort(a + 1, a + cnt + 1);
  a[0].p = a[cnt].p - m;
  a[cnt + 1].p = a[1].p + m;
  for (register long long i = (1); i <= (cnt); ++i)
    dis = min(dis + n * (a[i].p - a[i - 1].p), a[i].dis);
  for (register long long i = (1); i <= (cnt); ++i) {
    dis = min(dis + n * (a[i].p - a[i - 1].p), a[i].dis);
    if (a[i].p == a[i + 1].p) continue;
    if (a[i].id > g.size()) res = max(res, dis);
    if (a[i].p + 1 < a[i + 1].p)
      res = max(res, dis + n * (a[i + 1].p - a[i].p - 1));
  }
  return res;
}
signed main() {
  n = read(), m = read(), exgcd(m, n, x, y, d), x = (x % n + n) % n,
  y = (y % m + m) % m;
  if (d > 200000) return puts("-1") & 0;
  long long k = read(), t;
  while (k--) t = read(), b[t % d].push_back(t / d);
  k = read();
  while (k--) t = read(), g[t % d].push_back(t / d);
  long long res = 0;
  for (register long long i = (0); i <= (d - 1); ++i) {
    if (g[i].size() + b[i].size() == 0) return puts("-1") & 0;
    res = max(res, i + d * max(solve(m / d, n / d, y, g[i], b[i]),
                               solve(n / d, m / d, x, b[i], g[i])));
  }
  cout << res;
  return 0;
}
