#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<map>
#include<cassert>
#include<functional>
#include<queue>
#pragma warning(disable:4996)
using namespace std;
//#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
const int mod = 1000000007;
const long long inf = 1ll << 61;
const int smallinf = 1 << 30;
typedef pair<int, int> P;
typedef pair<int, P>PP;
int a[100006], b[100006], c[100006], d[100006];
int u[200006], v[200006];
vector<int>G[300006];//G[k]:kがつながっている辺の番号
int dis[200006];
int V;

signed main() {
	int x, y, z, n, m, s, t; 
	scanf("%d%d%d%d%d%d%d",&x,&y,&z,&n,&m,&s,&t);
	
	rep(i, n) {
		scanf("%d%d", &a[i], &b[i]);
		a[i]--; b[i]--;
	}
	rep(i, m) {
		scanf("%d%d", &c[i], &d[i]);
		c[i]--; d[i]--;
	}

	rep(i, n) {
		u[i] = a[i];
		v[i] = x + b[i];
		G[u[i]].push_back(i);
		G[v[i]].push_back(i);
	}
	rep(i, m) {
		u[n + i] = c[i] + x;
		v[n + i] = d[i] + x + y;
		G[u[n + i]].push_back(i + n);
		G[v[n + i]].push_back(i + n);
	}
	V = x + y + z;
	s--; t--; t += n;
	queue<P>Q;
	Q.push(P(0, s));
	fill(dis, dis + n+m, smallinf);
	dis[s] = 0;
	while (Q.size()) {
		P p = Q.front();
		Q.pop();
		int nv = p.second;
		if (dis[nv] < p.first)continue;
		for (int e : G[u[nv]]) {
			if (dis[e] > dis[nv] + 1) {
				dis[e] = dis[nv] + 1;
				Q.push(P(dis[e], e));
				if (e == t) {
					printf("%d\n", dis[e]); return 0;
				}
			}
		}
		for (int e : G[v[nv]]) {
			if (dis[e] > dis[nv] + 1) {
				dis[e] = dis[nv] + 1;
				Q.push(P(dis[e], e));
				if (e == t) {
					printf("%d\n", dis[e]); return 0;
				}
			}
		}
	}

	puts("-1");
}
