#include <bits/stdc++.h>
using namespace std;
vector<int> boys, girls;
int main() {
  int N, M, b, g;
  cin >> N >> M;
  boys.assign(N, 0);
  girls.assign(M, 0);
  cin >> b;
  for (int i = 0; i < b; ++i) {
    int a;
    cin >> a;
    boys[a] = true;
  }
  cin >> g;
  for (int i = 0; i < g; ++i) {
    int a;
    cin >> a;
    girls[a] = true;
  }
  int ans = N + M - b - g;
  for (int i = 0; i < max(N, M) * max(N, M); ++i) {
    if (girls[i % M] == boys[i % N]) continue;
    girls[i % M] = boys[i % N] = 1;
    ans--;
  }
  if (ans)
    cout << "NO\n";
  else
    cout << "YES\n";
}
