#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
long long x, y;
long long ex_gcd(long long a, long long b) {
  if (!b) {
    x = 1, y = 0;
    return a;
  }
  long long d = ex_gcd(b, a % b);
  long long tmp = x;
  x = y;
  y = tmp - a / b * y;
  return d;
}
vector<int> a[N], b[N];
long long ans = 0;
void solve(vector<int> &a, vector<int> &b, int n, int m, int g, int st) {
  if (a.size() == n / g) return;
  map<int, int> mi;
  vector<int> v;
  set<int> s;
  for (int i = 0; i < b.size(); i++) {
    int x = b[i] % n;
    if (mi.count(x)) {
      mi[x] = min(mi[x], b[i]);
    } else {
      v.push_back(x);
      mi[x] = b[i];
    }
  }
  for (int i = 0; i < a.size(); i++) {
    s.insert(a[i]);
    if (mi.count(a[i])) {
      mi[a[i]] = min(mi[x], a[i]);
    } else {
      v.push_back(a[i]);
      mi[a[i]] = a[i];
    }
  }
  ex_gcd(m, n);
  int loop = n / g;
  vector<pair<int, int> > pt;
  for (int i = 0; i < v.size(); i++) {
    y = 1LL * (v[i] - st) / g * x % loop;
    if (y < 0) y += loop;
    pt.push_back(make_pair(y, v[i]));
  }
  sort(pt.begin(), pt.end());
  if (pt.size() == 1) {
    ans = max(ans, 1LL * (loop - 1) * m + pt[0].second);
    return;
  }
  for (int i = 0; i < pt.size(); i++) {
    int j = (i + 1) % pt.size();
    int len = pt[j].first - pt[i].first - 1;
    if (len < 0) len += loop;
    ans = max(ans, 1LL * len * m + pt[i].second);
  }
}
int main() {
  int n, m;
  ios::sync_with_stdio(false);
  cin >> n >> m;
  int g = gcd(n, m);
  if (g >= N) {
    cout << -1 << endl;
    return 0;
  }
  int nn;
  cin >> nn;
  for (int i = 0; i < nn; i++) {
    int x;
    cin >> x;
    a[x % g].push_back(x);
  }
  int mm;
  cin >> mm;
  for (int i = 0; i < mm; i++) {
    int x;
    cin >> x;
    b[x % g].push_back(x);
  }
  for (int i = 0; i < g; i++) {
    if (!a[i].size() && !b[i].size()) {
      cout << -1 << endl;
      return 0;
    }
    solve(a[i], b[i], n, m, g, i);
    solve(b[i], a[i], m, n, g, i);
  }
  cout << ans << endl;
  return 0;
}
