#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int D, n[2], m[2], inv[2];
long long ans;
map<int, long long> d;
map<int, int> pos, tl, tr;
vector<int> g[2], a[2][N];
void exgcd(int a, int b, int &d, int &x, int &y) {
  if (!b) {
    x = 1, y = 0;
    d = a;
    return;
  }
  exgcd(b, a % b, d, x, y);
  int t = x;
  x = y;
  y = t - a / b * y;
}
bool cmp(int x, int y) { return pos[x] < pos[y]; }
long long solve(int type, int k) {
  vector<pair<int, int> > p;
  vector<int> l;
  for (auto i : a[type][k]) d[i] = inf, pos[i] = 1ll * i * inv[type] % n[type];
  for (auto i : a[type ^ 1][k])
    d[i % n[type]] = inf,
          pos[i % n[type]] = 1ll * i % n[type] * inv[type] % n[type];
  for (auto i : a[type][k]) d[i] = k + D * i, l.push_back(i);
  for (auto i : a[type ^ 1][k])
    d[i % n[type]] = min(d[i % n[type]], k + D * i * 1ll),
          l.push_back(i % n[type]);
  sort(l.begin(), l.end());
  l.erase(unique(l.begin(), l.end()), l.end());
  sort(l.begin(), l.end(), cmp);
  for (int i = 0; i < l.size(); ++i) {
    tl[l[i]] = l[(i - 1 + l.size()) % l.size()];
    tr[l[i]] = l[(i + 1) % l.size()];
  }
  queue<int> q;
  for (auto i : a[type][k]) q.push(i);
  while (q.size()) {
    int x = q.front();
    q.pop();
    long long t = pos[tr[x]] - pos[x];
    if (t < 0) t = -t + n[type];
    t *= 1ll * D * n[type ^ 1];
    if (d[x] + t < d[tr[x]]) d[tr[x]] = d[x] + t, q.push(tr[x]);
  }
  long long ret = 0;
  for (int i = 0; i < l.size(); ++i) {
    long long t = abs(pos[tr[l[i]]] - pos[l[i]]);
    if (i == l.size() - 1) t = n[type] - t;
    if (t == 1) continue;
    ret = max(ret, d[l[i]] + D * (t - 1) * n[type ^ 1]);
  }
  for (auto i : a[type][k]) d[i] = 0;
  for (auto i : a[type ^ 1][k]) ret = max(ret, d[i % n[type]]);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n[0] >> n[1];
  D = gcd(n[0], n[1]);
  for (int k = 0; k < 2; ++k) {
    cin >> m[k];
    for (int i = 0; i < m[k]; ++i) {
      int x;
      cin >> x;
      g[k].push_back(x);
    }
  }
  if (m[0] + m[1] < D) {
    puts("-1");
    return 0;
  }
  n[0] /= D, n[1] /= D;
  for (int k = 0; k < 2; ++k) {
    int d, x, y;
    exgcd(n[k ^ 1], n[k], d, x, y);
    x = (x % n[k] + n[k]) % n[k];
    inv[k] = x;
    for (int i = 0; i < m[k]; ++i) {
      int x = g[k][i];
      a[k][x % D].push_back(x / D);
    }
  }
  bool fg = true;
  for (int i = 0; i < D; ++i) {
    if (a[0][i].size() + a[1][i].size() == 0) {
      fg = false;
      break;
    } else
      for (int k = 0; k < 2; ++k) ans = max(ans, solve(k, i));
  }
  if (fg)
    printf("%lld\n", ans);
  else
    puts("-1");
  return 0;
}
