#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  T f = 0, x = 0;
  char c = getchar();
  while (!isdigit(c)) f = c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
namespace run {
const long long N = 2e5 + 9, inf = 1e12;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline void exgcd(long long a, long long b, long long &d, long long &x,
                  long long &y) {
  if (!b)
    return d = a, x = 1, y = 0, void();
  else
    exgcd(b, a % b, d, y, x), y -= (a / b) * x;
}
long long n, m, B, G, b[N], g[N], _gcd, ans, top;
vector<long long> by[N], gl[N];
pair<long long, long long> st[N];
inline void solve(long long tim) {
  if (!by[tim].size() && !gl[tim].size()) puts("-1"), exit(0);
  if (by[tim].size() == n && gl[tim].size() == m) return;
  if (n > m && by[tim].size() == n) return ans = max(ans, m - 1), void();
  long long d, x, y, ret = 0;
  exgcd(m, n, d, x, y), top = 0, x = (x + n) % n;
  for (long long i = 0; i < by[tim].size(); i++)
    st[++top] = pair<long long, long long>(by[tim][i] * x % n, by[tim][i]);
  for (long long i = 0; i < gl[tim].size(); i++)
    st[++top] =
        pair<long long, long long>((gl[tim][i] % m) * x % n, gl[tim][i]);
  for (long long i = 1; i <= top; i++) st[i].first = (st[i].first + n) % n;
  sort(st + 1, st + top + 1);
  for (long long i = 2; i <= top; i++)
    if (st[i].first == st[i - 1].first) st[i].second = st[i - 1].second;
  top = unique(st + 1, st + top + 1) - st - 1;
  long long las = 1;
  for (long long i = 2; i <= top; i++) {
    if (st[i - 1].first + 1 != st[i].first) {
      ret = max(ret, st[las].second + (st[i].first - 1 - st[las].first) * m);
    }
    if (st[las].second + (st[i].first - st[las].first) * m >= st[i].second)
      las = i;
  }
  if ((st[top].first + 1) % n != st[1].first)
    ret = max(ret, st[las].second +
                       ((st[1].first + n - 1) % n - st[las].first + n) % n * m);
  ans = max(ans, ret * _gcd + tim);
}
long long main() {
  n = read<long long>(), m = read<long long>();
  B = read<long long>();
  for (long long i = 1; i <= B; i++) b[i] = read<long long>();
  G = read<long long>();
  for (long long i = 1; i <= G; i++) g[i] = read<long long>();
  _gcd = gcd(n, m), n /= _gcd, m /= _gcd;
  if (_gcd > B + G) puts("-1"), exit(0);
  for (long long i = 1; i <= B; i++) by[b[i] % _gcd].push_back(b[i] / _gcd);
  for (long long i = 1; i <= G; i++) gl[g[i] % _gcd].push_back(g[i] / _gcd);
  for (long long i = 0; i < _gcd; i++) solve(i);
  swap(n, m);
  for (long long i = 0; i < _gcd; i++) swap(by[i], gl[i]), solve(i);
  printf("%lld\n", ans);
  return 0;
}
}  // namespace run
int main() { return run::main(); }
