#include <bits/stdc++.h>
using namespace std;
const int N = 300000;
int n, m, x, y, g;
int r, c;
int ai[N];
int exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  } else {
    int xx, yy, g = exgcd(b, a % b, xx, yy);
    x = yy;
    y = xx - a / b * yy;
    return g;
  }
}
map<int, vector<pair<int, pair<int, int> > > > L;
long long ans;
void solve(int flag) {
  L.clear();
  for (int i = 1; i <= r + c; ++i) {
    int a = ai[i];
    L[a % g].push_back(
        make_pair((1ll * (a / g) * y % (n / g) + (n / g)) % (n / g),
                  make_pair(a, flag ^ (i <= r))));
  }
  for (int i = 0; i < g; ++i)
    if (L[i].empty())
      cout << -1 << endl, exit(0);
    else {
      vector<pair<int, pair<int, int> > > V;
      sort(L[i].begin(), L[i].end());
      auto q = make_pair(-1, make_pair(-1, -1));
      for (auto p : L[i])
        if (p.first != q.first) V.push_back(p), q = p;
      q = make_pair(-1, make_pair(-1, -1));
      for (auto p : V) {
        if (q.first != -1)
          if (p.first - q.first - 1 || q.second.second)
            ans = max(ans, 1ll * (p.first - q.first - 1) * m + q.second.first);
        q = p;
      }
      if (L[i].begin()->first - q.first + (n / g) - 1 || q.second.second)
        ans = max(ans, 1ll * (V.begin()->first - q.first + (n / g) - 1) * m +
                           q.second.first);
    }
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  g = exgcd(n, m, x, y);
  cin >> r;
  for (int i = 1; i <= r; ++i) cin >> ai[i];
  cin >> c;
  for (int i = 1; i <= c; ++i) cin >> ai[r + i];
  solve(1);
  swap(n, m);
  swap(x, y);
  solve(0);
  cout << ans << endl;
}
