#include #include #include #include #define rep(i, n) for(i = 0; i < n; i++) #define int long long using namespace std; using namespace atcoder; const int INF = 1e+9; int n, a; int b[50]; int c; int d[50]; int xs[50], ys[50]; signed main() { int i, j; cin >> n >> a; rep(i, a) cin >> b[i]; cin >> c; rep(i, c) cin >> d[i]; sort(b, b + a, greater()); sort(d, d + c); rep(i, n) xs[i] = b[i % a]; rep(i, n) ys[i] = d[i % c]; mf_graph g(2 * n + 2); int s = 2 * n, t = s + 1; rep(i, n) { int lx = (i / a) * a, rx = lx + a; rep(j, n) { int ly = (i / c), ry = ly + c; int l = max(lx, ly); int r = min(rx, ry); if (l < r && xs[i] > ys[j]) { g.add_edge(i, j + n, 1); } } } rep(i, n) g.add_edge(s, i, 1); rep(i, n) g.add_edge(n + i, t, 1); cout << g.flow(s, t) << endl; return 0; }