#include using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);++i) #define REPR(i,n) for (int i=(int)(n)-1;i>=0;--i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() #define valid(y,x,h,w) (0<=y&&y pii; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (bbasic_ostream& operator<<(basic_ostream&os, const C& c){os<<'[';for(auto i=begin(c);i!=end(c);++i)os<<(i==begin(c)?"":" ")<<*i;return os<<']';} templateostream&operator<<(ostream &o,const pair&t){return o<<'('<void output(ostream&,const Tp&){} templatevoid output(ostream &o,const Tp&t){if(N)o<<',';o<(t);output(o,t);} templateostream&operator<<(ostream&o,const tuple&t){o<<'(';output<0,tuple,Ts...>(o,t);return o<<')';} templatevoid output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20]; int k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);} templatevoid outputs(T t){output(t);} templatevoid outputs(S a,T...t){output(a,32);outputs(t...);} templatevoid output(T *a,int n){REP(i,n)cout<void output(T *a,int n,int m){REP(i,n)output(a[i],m);} templatebool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;); if(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;} templatebool input(S&a,T&...t){input(a);return input(t...);} templatebool inputs(T *a, int n) { REP(i,n) if(!input(a[i])) return 0; return 1;} typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) { } }; typedef vector Edges; typedef vector Graph; typedef vector Array; typedef vector Matrix; void add_edge(Graph &g, int s, int d, Weight w){ g[s].push_back(Edge(s,d,w)); g[d].push_back(Edge(d,s,0)); } #define RESIDUE(s,t) (capacity[s][t]-flow[s][t]) Weight Dinic(const Graph &g, int s, int t) { int n = g.size(); Matrix flow(n, Array(n)), cap(n, Array(n)); // adj. matrix REP(u,n) FOR(e,g[u]) cap[e->src][e->dst] += e->weight; auto residue = [&](int s, int t) {return cap[s][t] - flow[s][t];}; Weight total = 0; for (bool cont = 1; cont; ) { cont = 0; vector level(n, -1); level[s] = 0; // make layered network queue Q({s}); for (int d = n; !Q.empty() && level[Q.front()] < d; ) { int u = Q.front(); Q.pop(); if (u == t) d = level[u]; FOR(e, g[u]) if (residue(u,e->dst) > 0 && level[e->dst] == -1) Q.push(e->dst), level[e->dst] = level[u] + 1; } vector used(n); // make blocking flows function augment = [&](int u, int cur) { if (u==t||cur==0) return cur; if (used[u]) return 0; used[u] = 1; FOR(e,g[u]) if (level[e->dst] > level[u]) { Weight f = augment(e->dst, min(cur, residue(u,e->dst))); if (f > 0) { flow[u][e->dst] += f; flow[e->dst][u] -= f; used[u] = 0; return f; } } return 0; }; for (Weight f = 1; f > 0; ) { f = augment(s, INF); if (f == 0) break; total += f; cont = 1; } } return total; } int B[50]; int D[50]; int l1[50], r1[50]; int l2[50], r2[50]; int main() { int n; while(input(n)) { int A; cin >> A; inputs(B,A); int C; cin >> C; inputs(D,C); REP(i,n) { l1[i] = (i / A) * A; r1[i] = (i / A + 1) * A; l2[i] = (i / C) * C; r2[i] = (i / C + 1) * C; } int N = r1[n-1]; int M = r2[n-1]; set S; REP(i,N) { for (int j=l2[i]; j D[y%C]) { add_edge(g,x,N+y,1); } } cout << Dinic(g,N+M,N+M+1) << endl; } }