#include <bits/stdc++.h>
using namespace std;
bool pos(vector<bool>& xais, vector<bool>& xaies, int n, int m) {
  for (int i = 0; i <= n * m; ++i) {
    if (xais[i % n] or xaies[i % m]) {
      xais[i % n] = true;
      xaies[i % m] = true;
    }
  }
  for (int i = 0; i <= n * m; ++i) {
    if (xais[i % n] or xaies[i % m]) {
      xais[i % n] = true;
      xaies[i % m] = true;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (not xais[i]) return false;
  }
  for (int j = 0; j < m; ++j) {
    if (not xaies[j]) return false;
  }
  return true;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<bool> xais(n);
  vector<bool> xaies(m);
  int c;
  int b;
  cin >> b;
  for (int i = 0; i < b; ++i) {
    cin >> c;
    xais[c] = true;
  }
  int g;
  cin >> g;
  for (int j = 0; j < g; ++j) {
    cin >> c;
    xaies[c] = true;
  }
  if (pos(xais, xaies, n, m))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
