#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
  x *= f;
}
const int N = 2e5 + 10;
struct node {
  int x, t;
  node(int x = 0, int t = 0) : x(x), t(t) {}
};
long long d[N], p[N];
int pos[N], ptot;
bool cmp(int x, int y) {
  if (p[x] != p[y]) return p[x] < p[y];
  return d[x] < d[y];
}
int vis[N];
long long solve(vector<int> &A, vector<int> &B, int n, int m, int inv) {
  if (A.size() == n) return -n;
  ptot = 0;
  for (int i = 1; i <= A.size() + B.size(); ++i) vis[i] = 0;
  for (int x : A)
    p[++ptot] = 1ll * x * inv % n, d[ptot] = x, pos[ptot] = ptot, vis[ptot] = 1;
  for (int x : B) p[++ptot] = 1ll * x * inv % n, d[ptot] = x, pos[ptot] = ptot;
  sort(pos + 1, pos + ptot + 1, cmp);
  int tmp_tot = 0;
  for (int i = 1; i <= ptot; ++i)
    if (i == 1 || p[pos[i]] != p[pos[i - 1]]) pos[++tmp_tot] = pos[i];
  p[pos[0] = 0] = p[pos[tmp_tot]] - n,
             p[pos[tmp_tot + 1] = ptot + 1] = p[pos[1]] + n;
  ptot = tmp_tot;
  long long dis = 1e18;
  for (int i = 1; i <= ptot; ++i)
    dis = min(d[pos[i]], dis + (p[pos[i]] - p[pos[i - 1]]) * m);
  long long ans = 0;
  for (int i = 1; i <= ptot; ++i) {
    dis = min(d[pos[i]], dis + (p[pos[i]] - p[pos[i - 1]]) * m);
    if (p[pos[i + 1]] - 1 - p[pos[i]] > 0)
      ans = max(ans, (p[pos[i + 1]] - 1 - p[pos[i]]) * m + dis);
    if (!vis[pos[i]]) ans = max(ans, dis);
  }
  return ans;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b) return (x = 1, y = 0, a);
  long long g = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return g;
}
long long getinv(long long a, long long b) {
  long long x1, x2;
  exgcd(a, b, x1, x2);
  return (x1 % b + b) % b;
}
vector<int> A[N], B[N];
int n, m, a, b;
int xi[N], yi[N];
int main() {
  rd(n), rd(m);
  rd(a);
  for (int i = 1; i <= a; ++i) rd(xi[i]);
  rd(b);
  for (int i = 1; i <= b; ++i) rd(yi[i]);
  int d = gcd(n, m);
  if (d > a + b) (printf("-1"), exit(0));
  int InvN = getinv(n / d, m / d);
  int InvM = getinv(m / d, n / d);
  for (int i = 1; i <= a; ++i) A[xi[i] % d].push_back(xi[i] / d);
  for (int i = 1; i <= b; ++i) B[yi[i] % d].push_back(yi[i] / d);
  long long ans = 0;
  for (int u = 0; u < d; ++u) {
    if (!A[u].size() && !B[u].size()) (printf("-1"), exit(0));
    ans = max(ans, solve(A[u], B[u], n / d, m / d, InvM) * d + u);
    ans = max(ans, solve(B[u], A[u], m / d, n / d, InvN) * d + u);
  }
  printf("%lld", ans);
  return 0;
}
