#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int x = ri(), y = ri(), z = ri();
	int n = ri(), m = ri();
	int s = ri() - 1, t = ri() - 1;
	std::vector<int> hen[x + y + z + n + m];
	for (int i = 0; i < n; i++) {
		int a = ri() - 1;
		int b = ri() - 1 + x;
		int t = x + y + z + i;
		hen[t].push_back(a);
		hen[t].push_back(b);
		hen[a].push_back(t);
		hen[b].push_back(t);
	}
	for (int i = 0; i < m; i++) {
		int a = ri() - 1 + x;
		int b = ri() - 1 + x + y;
		int t = x + y + z + n + i;
		hen[t].push_back(a);
		hen[t].push_back(b);
		hen[a].push_back(t);
		hen[b].push_back(t);
	}
	std::vector<int> dist(x + y + z + n + m, 1000000000);
	std::queue<int> que;
	que.push(x + y + z + s);
	dist[x + y + z + s] = 0;
	while (que.size()) {
		int cur = que.front();
		que.pop();
		for (auto j : hen[cur]) {
			if (dist[j] > dist[cur] + 1) {
				dist[j] = dist[cur] + 1;
				que.push(j);
			}
		}
	}
	int res = dist[x + y + z + n + t];
	std::cout << (res == 1000000000 ? -1 : res / 2) << std::endl;
	return 0;
}


