#include <bits/stdc++.h>
using namespace std;
long long func(long long cycle, long long cost, vector<pair<int, int> > init) {
  int i, j;
  sort(init.begin(), init.end());
  vector<pair<long long, long long> > v;
  int sz = init.size();
  i = 0;
  while (i < sz) {
    long long best = (1ll << 61);
    for (j = i; j < sz; j++) {
      if (init[j].first != init[i].first) break;
      best = min(best, (long long)init[j].second);
    }
    v.push_back(make_pair((long long)init[i].first, best));
    i = j;
  }
  long long ans = 0;
  sz = v.size();
  for ((i) = 0; (i) < (int)(sz); (i)++) {
    long long tmp;
    if (i == sz - 1) {
      tmp = v[0].first - v[sz - 1].first + cycle;
    } else {
      tmp = v[i + 1].first - v[i].first;
    }
    ans = max(ans, (tmp - 1) * cost + v[i].second);
  }
  return ans;
}
int gcd(int x, int y) { return x ? gcd(y % x, x) : y; }
long long power(long long x, long long n, long long mod) {
  if (n == 0) return 1;
  long long y = power(x, n / 2, mod);
  y = y * y % mod;
  if (n % 2 == 1) y = y * x % mod;
  return y;
}
long long phi(long long n) {
  long long ans = n;
  long long x = n;
  for (long long p = 2; p * p <= x; p++)
    if (x % p == 0) {
      while (x % p == 0) x /= p;
      ans -= ans / p;
    }
  if (x != 1) ans -= ans / x;
  return ans;
}
vector<pair<int, int> > v[200010];
long long func(int X, int Y, vector<int> vx, vector<int> vy) {
  int i;
  int g = gcd(X, Y);
  if (g > 200005) return (1ll << 61);
  long long r = Y / g;
  long long mod = X / g;
  long long yinv = 1;
  if (mod != 1) yinv = power(r, phi(mod) - 1, mod);
  for ((i) = 0; (i) < (int)(g); (i)++) v[i].clear();
  for ((i) = 0; (i) < (int)(vx.size()); (i)++) {
    int x = vx[i];
    int t = vx[i];
    long long id = (x / g) * yinv % (X / g);
    v[x % g].push_back(make_pair((int)id, t));
  }
  for ((i) = 0; (i) < (int)(vy.size()); (i)++) {
    int x = vy[i] % X;
    int t = vy[i];
    long long id = (x / g) * yinv % (X / g);
    v[x % g].push_back(make_pair((int)id, t));
  }
  for ((i) = 0; (i) < (int)(g); (i)++)
    if (v[i].empty()) return (1ll << 61);
  long long ans = 0;
  for ((i) = 0; (i) < (int)(g); (i)++) {
    long long tmp = func(X / g, Y, v[i]);
    ans = max(ans, tmp);
  }
  return ans;
}
int tx[1000010];
int ty[1000010];
vector<int> input(void) {
  int sz, x, i;
  cin >> sz;
  vector<int> v;
  for ((i) = 0; (i) < (int)(sz); (i)++) {
    scanf("%d", &x);
    v.push_back(x);
  }
  return v;
}
int main(void) {
  int X, Y, i;
  cin >> X >> Y;
  vector<int> vx = input();
  vector<int> vy = input();
  int M = 1000000;
  if (max(X, Y) <= M) {
    for ((i) = 0; (i) < (int)(X); (i)++) tx[i] = (1 << 29);
    for ((i) = 0; (i) < (int)(Y); (i)++) ty[i] = (1 << 29);
    for ((i) = 0; (i) < (int)(vx.size()); (i)++) tx[vx[i]] = -1;
    for ((i) = 0; (i) < (int)(vy.size()); (i)++) ty[vy[i]] = -1;
    for ((i) = 0; (i) < (int)(M); (i)++)
      if (tx[i % X] < i || ty[i % Y] < i) {
        tx[i % X] = min(tx[i % X], i);
        ty[i % Y] = min(ty[i % Y], i);
      }
    int ans = 0;
    for ((i) = 0; (i) < (int)(X); (i)++) ans = max(ans, tx[i]);
    for ((i) = 0; (i) < (int)(Y); (i)++) ans = max(ans, ty[i]);
    if (ans != (1 << 29)) {
      cout << ans << endl;
      return 0;
    }
  }
  long long ans1 = func(X, Y, vx, vy);
  long long ans2 = func(Y, X, vy, vx);
  long long ans = max(ans1, ans2);
  if (ans == (1ll << 61)) ans = -1;
  cout << ans << endl;
  return 0;
}
