#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int M = N + 7;
const int MOD = 1e9 + 7;
const long long INF = 1e18 + 17;
int gcd(int a, int b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  int mn = gcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return mn;
}
vector<int> pos[2][M];
long long getAns(int id1, int n, long long x, int id2, int m, long long y,
                 int mn, int w) {
  if (pos[id1][w].empty() && pos[id2][w].empty()) return INF;
  if ((int)pos[id1][w].size() == n / mn) return 0;
  int l = n / mn;
  set<int> s;
  for (int p : pos[id1][w]) s.insert(p);
  for (int p : pos[id2][w]) s.insert(p % n);
  vector<pair<int, int> > points;
  for (int v : s) {
    int p = 1LL * y * (v / mn) % l;
    p = (p < 0 ? p + l : p);
    points.push_back(make_pair(p, v));
  }
  sort(points.begin(), points.end());
  long long res = 0;
  for (int i = 0; i < (int)points.size(); ++i) {
    int j = (i + 1) % (int)points.size();
    int len = points[j].first - points[i].first - 1;
    len = (len < 0 ? len + l : len);
    res = max(res, 1LL * len * m + points[i].second);
  }
  return res;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  long long x, y;
  int mn = gcd(n, m, x, y);
  if (mn > N) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i < 2; ++i) {
    int w;
    scanf("%d", &w);
    for (int j = 0; j < w; ++j) {
      int v;
      scanf("%d", &v);
      pos[i][v % mn].push_back(v);
    }
  }
  long long ans = 0;
  for (int i = 0; i < mn; ++i) {
    long long res =
        max(getAns(0, n, x, 1, m, y, mn, i), getAns(1, m, y, 0, n, x, mn, i));
    ans = max(ans, res);
  }
  if (ans == INF)
    printf("-1\n");
  else
    printf("%lld\n", ans);
  return 0;
}
