#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int rd() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * w;
}
int ggcd(int a, int b) { return b ? ggcd(b, a % b) : a; }
void exgcd(int a, int b, int &d, int &x, int &y) {
  if (!b) {
    d = a, x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, d, y, x), y -= a / b * x;
}
int cal(int a, int z, int b) {
  int x, y, d;
  exgcd(a, b, d, x, y);
  return 1ll * (x % b + b) % b * (z / d) % b;
}
int n, m, gg, a[N], ta, b[N], tb;
struct node {
  long long x, t;
  bool operator<(const node &bb) const {
    return x != bb.x ? x < bb.x : t < bb.t;
  }
} s1[N], s2[N], las;
int t1, t2;
set<node> sss;
set<node>::iterator it, ft, nt;
vector<int> sa[N], sb[N];
map<int, bool> mp;
long long ans;
void wk(node *sq, int pt, int i, int n, int m) {
  sss.clear(), sss.insert((node){-1, -1}), sss.insert((node){1 << 30, -1});
  for (int j = 1; j <= pt; ++j) {
    node nw = sq[j];
    nw.x = cal(m, nw.x, n);
    nt = sss.upper_bound(nw), ft = nt, --ft;
    int rs = sss.size();
    while (rs > 2) {
      if (ft == sss.begin()) ft = --sss.end(), --ft;
      if (nt == --sss.end()) nt = sss.begin(), ++nt;
      long long ds = (*ft).x < nw.x ? nw.x - (*ft).x : nw.x + n - (*ft).x;
      if ((*ft).t + ds * m < nw.t) {
        nw.x = -1;
        break;
      }
      ds = nw.x < (*nt).x ? (*nt).x - nw.x : (*nt).x + n - nw.x;
      if (nw.t + ds * m < (*nt).t) {
        it = nt, ++nt;
        if (nt == --sss.end()) nt = sss.begin(), ++nt;
        sss.erase(it);
      } else
        break;
    }
    if (~nw.x) sss.insert(nw);
  }
  long long ns = 0;
  las.x = -1;
  for (it = sss.begin(); it != sss.end(); ++it) {
    node nw = *it;
    if (nw.x > n) break;
    if (~las.x) {
      long long ds = nw.x - las.x;
      ns =
          max(ns, (nw.x - las.x == 1 && (bool)mp.count(las.x) ? 0 : 1) * las.t +
                      (ds - 1) * m);
    }
    las = nw;
  }
  if (sss.size() > 3) {
    ft = --sss.end(), --ft;
    nt = sss.begin(), ++nt;
    long long ds = (*nt).x + n - (*ft).x;
    ns = max(ns, (ds == 1 && (bool)mp.count((*ft).x) ? 0 : 1) * las.t +
                     (ds - 1) * m);
  } else {
    it = sss.begin(), ++it;
    ns = (*it).t + 1ll * (n - 1) * m;
  }
  ans = max(ans, ns * gg + i);
}
int main() {
  n = rd(), m = rd();
  ta = rd();
  for (int i = 1; i <= ta; ++i) a[i] = rd();
  tb = rd();
  for (int i = 1; i <= tb; ++i) b[i] = rd();
  if (n > m) {
    swap(n, m), swap(ta, tb);
    for (int i = 1; i <= max(ta, tb); ++i) swap(a[i], b[i]);
  }
  gg = ggcd(n, m);
  if (gg > ta + tb) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= ta; ++i) sa[a[i] % gg].push_back(a[i] / gg);
  for (int i = 1; i <= tb; ++i) sb[b[i] % gg].push_back(b[i] / gg);
  s1[0].x = s2[0].x = -1;
  n /= gg, m /= gg;
  for (int i = 0; i < gg; ++i) {
    if (sa[i].empty() && sb[i].empty()) {
      puts("-1");
      return 0;
    }
    if ((int)sa[i].size() + (int)sb[i].size() == n + m) continue;
    t1 = t2 = 0;
    int nn = sa[i].size();
    for (int j = 0; j < nn; ++j) s2[++t2] = (node){sa[i][j], sa[i][j]};
    nn = sb[i].size();
    for (int j = 0; j < nn; ++j) s1[++t1] = (node){sb[i][j] % n, sb[i][j]};
    nn = sa[i].size();
    for (int j = 0; j < nn; ++j) s1[++t1] = (node){sa[i][j], sa[i][j]};
    nn = sb[i].size();
    for (int j = 0; j < nn; ++j) s2[++t2] = (node){sb[i][j], sb[i][j]};
    sort(s1 + 1, s1 + t1 + 1);
    nn = t1, t1 = 0;
    for (int j = 1; j <= nn; ++j)
      if (s1[j].x != s1[t1].x) s1[++t1] = s1[j];
    sort(s2 + 1, s2 + t2 + 1);
    nn = t2, t2 = 0;
    for (int j = 1; j <= nn; ++j)
      if (s2[j].x != s2[t2].x) s2[++t2] = s2[j];
    nn = sa[i].size(), mp.clear();
    for (int j = 0; j < nn; ++j) mp[cal(m, sa[i][j], n)] = 1;
    wk(s1, t1, i, n, m);
    nn = sb[i].size(), mp.clear();
    for (int j = 0; j < nn; ++j) mp[cal(n, sb[i][j], m)] = 1;
    wk(s2, t2, i, m, n);
  }
  printf("%lld\n", ans);
  return 0;
}
