#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct Node {
  long long f, p;
} c[200010 << 1];
vector<long long> a[200010], b[200010];
map<long long, bool> M;
long long n, m, Res, Ans, g, x, y;
int i, j, k, p, t, l;
inline long long Gcd(long long x, long long y) {
  return !y ? x : Gcd(y, x % y);
}
inline void Ex_gcd(long long a, long long b, long long& x, long long& y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  Ex_gcd(b, a % b, y, x);
  y -= a / b * x;
}
inline bool Cmp(Node a, Node b) { return a.p < b.p; }
inline void Solve(vector<long long> a, vector<long long> b, long long n,
                  long long m) {
  if (!a.size() && !b.size()) {
    Res = INF;
    return;
  }
  long long l1 = n / g, l2 = m / g;
  Ex_gcd(l1, l2, x, y);
  t = 0;
  (y += l1) %= l1;
  M.clear();
  for (int i = 0; i < a.size(); i++)
    if (!M[a[i]]) c[++t].f = a[i], c[t].p = a[i] * y % l1, M[a[i]] = 1;
  for (int i = 0; i < b.size(); i++)
    if (!M[b[i] % l1])
      c[++t].f = b[i] + INF, c[t].p = b[i] * y % l1, M[b[i] % l1] = 1;
  sort(c + 1, c + t + 1, Cmp);
  c[++t].p = c[1].p;
  for (int i = 1; i < t; i++) {
    long long u = c[i + 1].p - c[i].p - 1;
    if (u < 0) u += l1;
    if (!u && c[i].f < INF) continue;
    Res = max(Res, u * l2 + (c[i].f >= INF ? c[i].f - INF : c[i].f));
  }
}
int main() {
  scanf("%I64d%I64d", &n, &m);
  g = Gcd(n, m);
  if (g > 200000) {
    puts("-1");
    return 0;
  }
  scanf("%d", &l);
  while (l--) scanf("%I64d", &x), a[x % g].push_back(x / g);
  scanf("%d", &l);
  while (l--) scanf("%I64d", &x), b[x % g].push_back(x / g);
  for (i = 0; i < g; i++) {
    Res = -1;
    sort(a[i].begin(), a[i].end());
    sort(b[i].begin(), b[i].end());
    Solve(a[i], b[i], n, m);
    Solve(b[i], a[i], m, n);
    if (Res >= INF) {
      puts("-1");
      return 0;
    }
    if (Res >= 0) Ans = max(Ans, Res * g + i);
  }
  cout << Ans << endl;
  return 0;
}
