#include <bits/stdc++.h>
using namespace std;
vector<long long> va[200005], vb[200005];
vector<pair<long long, long long> > vec;
pair<long long, long long> sta[200005];
long long n, m, A_, B_, ans, a[200005], b[200005], A, B, d, top;
void Fail() {
  printf("-1");
  exit(0);
}
long long gcd(long long x, long long y) {
  if (!y) return x;
  return gcd(y, x % y);
}
void exgcd(long long x, long long y, long long &a, long long &b) {
  if (!y) {
    a = 1, b = 0;
    return;
  }
  exgcd(y, x % y, a, b);
  long long t = b;
  b = a - b * (x / y);
  a = t;
}
long long inv(long long x, long long y) {
  long long a = 0, b = 0;
  exgcd(x, y, a, b);
  return (a % y + y) % y;
}
long long solve(long long id) {
  if (!va[id].size() && !vb[id].size()) Fail();
  if (va[id].size() == A_ && vb[id].size() == B_) {
    return 0;
  }
  long long ans = 0;
  top = 0;
  vec.clear();
  for (long long i = 0; i < va[id].size(); i++) {
    vec.push_back(make_pair((va[id][i] - id) / d, -va[id][i]));
  }
  for (long long i = 0; i < vb[id].size(); i++) {
    vec.push_back(make_pair((vb[id][i] - id) / d % A_, vb[id][i]));
  }
  sort(vec.begin(), vec.end());
  for (long long i = 0; i < vec.size(); i++) {
    if (vec[i].first == sta[top].first && top) continue;
    sta[++top] = vec[i];
  }
  long long iv = inv(B_, A_);
  for (long long i = top; i >= 1; i--) {
    sta[i].first = (sta[i].first - sta[1].first) * iv % A_;
  }
  sort(sta + 1, sta + top + 1);
  sta[top + 1].first = A_;
  for (long long i = 1; i <= top; i++) {
    if (sta[i].second > 0) ans = max(ans, sta[i].second);
    if (sta[i + 1].first - sta[i].first > 1) {
      ans = max(ans,
                abs(sta[i].second) + (sta[i + 1].first - sta[i].first - 1) * m);
    }
  }
  top = 0;
  vec.clear();
  for (long long i = 0; i < vb[id].size(); i++) {
    vec.push_back(make_pair((vb[id][i] - id) / d, -vb[id][i]));
  }
  for (long long i = 0; i < va[id].size(); i++) {
    vec.push_back(make_pair((va[id][i] - id) / d % B_, va[id][i]));
  }
  sort(vec.begin(), vec.end());
  for (long long i = 0; i < vec.size(); i++) {
    if (vec[i].first == sta[top].first && top) continue;
    sta[++top] = vec[i];
  }
  iv = inv(A_, B_);
  for (long long i = top; i >= 1; i--) {
    sta[i].first = (sta[i].first - sta[1].first) * iv % B_;
  }
  sort(sta + 1, sta + top + 1);
  sta[top + 1].first = B_;
  for (long long i = 1; i <= top; i++) {
    if (sta[i].second > 0) ans = max(ans, sta[i].second);
    if (sta[i + 1].first - sta[i].first > 1) {
      ans = max(ans,
                abs(sta[i].second) + (sta[i + 1].first - sta[i].first - 1) * n);
    }
  }
  return ans;
}
int main() {
  scanf("%lld%lld", &n, &m);
  d = gcd(n, m);
  scanf("%lld", &A);
  for (long long i = 1; i <= A; i++) scanf("%lld", &a[i]);
  scanf("%lld", &B);
  for (long long i = 1; i <= B; i++) scanf("%lld", &b[i]);
  if (d > A + B) {
    printf("-1");
    return 0;
  }
  A_ = n / d;
  B_ = m / d;
  for (long long i = 1; i <= A; i++) {
    va[a[i] % d].push_back(a[i]);
  }
  for (long long i = 1; i <= B; i++) {
    vb[b[i] % d].push_back(b[i]);
  }
  for (long long i = 0; i < d; i++) {
    ans = max(ans, solve(i));
  }
  printf("%lld", ans);
}
