#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const int MAX = 30010;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x;
}
int vis[MAX], n = read(), m = read(), p = read(), q = read(), x, y, ans1, ans2;
int find(int x) {
return vis[x] == x ? x : vis[x] = find(vis[x]);
}
void merge(int x, int y) {
if (find(x) != find(y))vis[find(x)] = find(y);
}
int main() {
for (int i = 1; i <= 2 * n; i++)vis[i] = i;
for (int i = 1; i <= p; i++) {
x = read(), y = read();
if (find(x) != find(y))merge(x, y);
}
for (int i = 1; i <= q; i++) {
x = read(), y = read();
if (find(abs(x) + n) != find(abs(y) + n))merge(abs(x) + n, abs(y) + n);
}
for (int i = 1; i <= n; i++)
if (find(i) == find(1))ans1++;
for (int i = n + 1; i <= m + n; i++)
if (find(i) == find(n + 1))ans2++;
cout << min(ans1, ans2);
return 0;
}
80分,wa了