#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct P {
  int x, k, d;
};
int gcd(int x, int y) {
  if (!y) return x;
  return gcd(y, x % y);
}
int exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  int g = exgcd(b, a % b, x, y);
  int tmp = x;
  x = y;
  y = tmp - (a / b) * y;
  return g;
}
set<int> kn[100010 * 2], km[100010 * 2];
struct X {
  int las, d, op;
} a[100010 * 2];
int top;
bool cmp(X a, X b) { return a.las < b.las; }
long long solve(int n, int m, int id) {
  map<int, int> dis;
  set<int> pos;
  for (int x : kn[id]) {
    pos.insert(x);
    dis[x] = x;
  }
  for (int x : km[id]) {
    pos.insert(x % n);
    if (dis.count(x % n))
      dis[x % n] = min(dis[x % n], x);
    else
      dis[x % n] = x;
  }
  int x, y;
  exgcd(m, n, x, y);
  x %= n;
  if (x < 0) x += n;
  top = 0;
  int st = (*pos.begin());
  for (int p : pos)
    a[++top] =
        (X){(int)(1ll * x * (p - st) % n), dis[p], (int)(kn[id].count(p))};
  sort(a + 1, a + top + 1, cmp);
  a[top + 1] = (X){n, -1, -1};
  long long res = -1e14;
  for (int i = 1; i <= top; i++)
    if (!a[i].op || a[i + 1].las > a[i].las + 1)
      res = max(res, a[i].d + 1ll * (a[i + 1].las - a[i].las - 1) * m);
  return res;
}
int main() {
  int n = read(), m = read();
  int g = gcd(n, m);
  if (g > 200000) {
    puts("-1");
    return 0;
  }
  int B = read();
  for (int i = 1; i <= B; i++) {
    int x = read();
    kn[x % g].insert(x / g);
  }
  int G = read();
  for (int i = 1; i <= G; i++) {
    int x = read();
    km[x % g].insert(x / g);
  }
  long long ans = 0;
  for (int i = 0; i < g; i++) {
    if (!kn[i].size() && !km[i].size()) {
      puts("-1");
      return 0;
    }
    ans = max(ans, solve(n / g, m / g, i) * g + i);
    swap(kn[i], km[i]);
    ans = max(ans, solve(m / g, n / g, i) * g + i);
  }
  cout << ans << endl;
  return 0;
}
