#include using namespace std; template struct mincostflow { private: int N; struct _edge { int to, rev; flow_t cap; cost_t cost; }; vector> Pos; vector> G; public: mincostflow() {} mincostflow(int N) : N(N), G(N) {} void add_edge(int from, int to, flow_t cap, cost_t cost) { Pos.push_back({from, int(G[from].size())}); int from_id = int(G[from].size()); int to_id = int(G[to].size()); if (from == to) to_id++; G[from].push_back(_edge{to, to_id, cap, cost}); G[to].push_back(_edge{from, from_id, 0, -cost}); } pair flow(int s, int t) { return flow(s, t, numeric_limits::max()); } pair flow(int s, int t, flow_t flow_limit) { return slope(s, t, flow_limit).back(); } vector> slope(int s, int t) { return slope(s, t, numeric_limits::max()); } vector> slope(int s, int t, flow_t flow_limit) { vector dual(N, 0), dist(N); vector pv(N), pe(N); vector vis(N); auto dual_ref = [&]() { fill(dist.begin(), dist.end(), numeric_limits::max()); fill(pv.begin(), pv.end(), -1); fill(pe.begin(), pe.end(), -1); fill(vis.begin(), vis.end(), false); struct Q { cost_t key; int to; bool operator<(Q r) const { return key > r.key; } }; priority_queue que; dist[s] = 0; que.push(Q{0, s}); while (!que.empty()) { int v = que.top().to; que.pop(); if (vis[v]) continue; vis[v] = true; if (v == t) break; for (int i = 0; i < int(G[v].size()); i++) { auto e = G[v][i]; if (vis[e.to] || !e.cap) continue; cost_t cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if (!vis[t]) return false; for (int v = 0; v < N; v++) { if (!vis[v]) continue; dual[v] -= dist[t] - dist[v]; } return true; }; flow_t flow = 0; cost_t cost = 0, prev_cost_per_flow = -1; vector> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!dual_ref()) break; flow_t c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) c = min(c, G[pv[v]][pe[v]].cap); for (int v = t; v != s; v = pv[v]) { auto& e = G[pv[v]][pe[v]]; e.cap -= c; G[v][e.rev].cap += c; } cost_t d = -dual[s]; flow += c; cost += c * d; if (prev_cost_per_flow == d) result.pop_back(); result.push_back({flow, cost}); prev_cost_per_flow = d; } return result; } }; int main() { int N, A, C; cin >> N >> A; vector B(A); for (int i = 0; i < A; i++) { cin >> B[i]; } cin >> C; vector D(C); for (int i = 0; i < C; i++) { cin >> D[i]; } sort(B.begin(), B.end(), greater<>()); sort(D.begin(), D.end()); mincostflow mcf(N * 2 + 2); int s = N * 2, t = N * 2 + 1; for (int i = 0; i < N; i++) { mcf.add_edge(s, i, 1, 0); mcf.add_edge(N + i, t, 1, 0); } for (int i = 0; i < N; i++) { int l = i / A * A; int r = min(l + A, N); l = l / C * C; r = min(r / C * C + C, N); for (int j = l; j < r; j++) { int cost = (B[i % A] <= D[j % C]); mcf.add_edge(i, N + j, 1, cost); } } cout << N - mcf.flow(s, t).second << endl; }