#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(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 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T>
inline void umin(T &x, T y) {
  x = x < y ? x : y;
}
template <class T>
inline void umax(T &x, T y) {
  x = x > y ? x : y;
}
inline unsigned int R() {
  static unsigned int seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 266666;
int n, m, g;
long long res;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
inline void exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}
struct Solver {
  int n, m;
  void ini(int nn, int mm) {
    n = nn;
    m = mm;
  }
  vector<pair<int, long long> > a[N];
  set<int> Set[N];
  inline void addans(int p, int time, int special) {
    int bel = p % g;
    int x, y;
    exgcd(m / g, n / g, x, y);
    x = 1LL * x * ((p - bel) / g) % (n / g);
    a[bel].push_back(pair<int, long long>(x, time));
    if (special == 0) Set[bel].insert(x);
  }
  void solve() {
    for (register int i = (0); i <= (g - 1); i++)
      if (!((int)a[i].size())) {
        puts("-1");
        exit(0);
      }
    for (register int i = (0); i <= (g - 1); i++)
      sort(a[i].begin(), a[i].end());
    for (register int j = (0); j <= (g - 1); j++) {
      int p = 0;
      for (register int i = (0); i <= (((int)a[j].size()) - 1); i++)
        if (a[j][i].second < a[j][p].second) p = i;
      for (int t = p;; t = t + 1 < ((int)a[j].size()) ? t + 1 : 0) {
        if (t + 1 < ((int)a[j].size()))
          umin(a[j][t + 1].second,
               a[j][t].second + 1LL * m * (a[j][t + 1].first - a[j][t].first));
        else
          umin(a[j][0].second,
               a[j][t].second +
                   1ll * m * (a[j][0].first - a[j][t].first + n / g));
        if ((t + 1 < ((int)a[j].size()) ? t + 1 : 0) == p) break;
      }
      for (register int i = (0); i <= (((int)a[j].size()) - 1); i++) {
        if (!Set[j].count(a[j][i].first)) umax(res, a[j][i].second);
        int t = i;
        if (i + 1 < ((int)a[j].size())) {
          if (a[j][i + 1].first - a[j][i].first > 1)
            umax(res, a[j][t].second +
                          1LL * m * (a[j][t + 1].first - a[j][t].first) - m);
        } else {
          if (a[j][0].first - a[j][t].first + n / g > 1)
            umax(res, a[j][t].second +
                          1ll * m * (a[j][0].first - a[j][t].first + n / g) -
                          m);
        }
      }
    }
  }
} A, B;
int main() {
  read(n);
  read(m);
  g = gcd(n, m);
  A.ini(n, m);
  B.ini(m, n);
  if (g > 2e5) {
    puts("-1");
    return 0;
  }
  int l;
  read(l);
  while (l--) {
    int x;
    read(x);
    A.addans(x, x, 0);
    B.addans(x % m, x, x);
  }
  read(l);
  while (l--) {
    int x;
    read(x);
    B.addans(x, x, 0);
    A.addans(x % n, x, x);
  }
  A.solve();
  B.solve();
  cout << res;
  return 0;
}
