#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

enum Type{
	KATSU_SAND,
	KATSU_CURRY,
};

struct Info{

	int sand,katsu,curry;
	Type type;
};

struct Data{

	Data(int arg_node_id,int arg_sum_dist){
		node_id = arg_node_id;
		sum_dist = arg_sum_dist;
	}
	bool operator<(const struct Data &arg) const{

		return sum_dist > arg.sum_dist; //総距離の降順(PQ)
	}
	int node_id,sum_dist;
};

int X,Y,Z,N,M;
int start,goal;
int num_info;
int min_dist[2*SIZE];
bool visited_SAND[SIZE],visited_KATSU[SIZE],visited_CURRY[SIZE];
Info info[2*SIZE];
vector<int> SAND[SIZE],KATSU[SIZE],CURRY[SIZE];


int main(){

	scanf("%d %d %d %d %d",&X,&Y,&Z,&N,&M);
	scanf("%d %d",&start,&goal);

	start--;
	goal = (goal-1)+N;

	//カツサンド
	for(int i = 0; i < N; i++){

		scanf("%d %d",&info[i].sand,&info[i].katsu);
		info[i].sand--;
		info[i].katsu--;
		info[i].type = KATSU_SAND;
		SAND[info[i].sand].push_back(i);
		KATSU[info[i].katsu].push_back(i);
	}

	//カツカレー
	for(int i = 0; i < M; i++){

		int index = N+i;
		scanf("%d %d",&info[index].katsu,&info[index].curry);
		info[index].katsu--;
		info[index].curry--;
		info[index].type = KATSU_CURRY;
		KATSU[info[index].katsu].push_back(index);
		CURRY[info[index].curry].push_back(index);
	}

	num_info = N+M;

	priority_queue<Data> Q;
	for(int i = 0; i < num_info; i++){

		min_dist[i] = BIG_NUM;
	}
	min_dist[start] = 0;

	//辿る未来は同じなので、遷移処理は一度だけで良い
	for(int i = 0; i < X; i++){

		visited_SAND[i] = false;
	}
	for(int i = 0; i < Y; i++){

		visited_KATSU[i] = false;
	}
	for(int i = 0; i < Z; i++){

		visited_CURRY[i] = false;
	}

	Q.push(Data(start,0));


	int next_node,next_dist;

	while(!Q.empty()){

		if(Q.top().sum_dist > min_dist[Q.top().node_id]){

			Q.pop();
		}else if(Q.top().node_id == goal){

			printf("%d\n",Q.top().sum_dist);
			return 0;

		}else{

			if(visited_KATSU[info[Q.top().node_id].katsu] == false){

				//カツは共通

				for(int i = 0; i < KATSU[info[Q.top().node_id].katsu].size(); i++){

					next_node = KATSU[info[Q.top().node_id].katsu][i];
					next_dist = Q.top().sum_dist+1;

					if(min_dist[next_node] > next_dist){
						min_dist[next_node] = next_dist;
						Q.push(Data(next_node,next_dist));
					}
				}

				visited_KATSU[info[Q.top().node_id].katsu] = true;
			}

			if(info[Q.top().node_id].type == KATSU_SAND){

				if(visited_SAND[info[Q.top().node_id].sand] == false){

					for(int i = 0; i < SAND[info[Q.top().node_id].sand].size(); i++){

						next_node = SAND[info[Q.top().node_id].sand][i];
						next_dist = Q.top().sum_dist+1;

						if(min_dist[next_node] > next_dist){
							min_dist[next_node] = next_dist;
							Q.push(Data(next_node,next_dist));
						}
					}

					visited_SAND[info[Q.top().node_id].sand] = true;
				}

			}else{ //KATSU_CURRY

				if(visited_CURRY[info[Q.top().node_id].curry] == false){

					for(int i = 0; i < CURRY[info[Q.top().node_id].curry].size(); i++){

						next_node = CURRY[info[Q.top().node_id].curry][i];
						next_dist = Q.top().sum_dist+1;

						if(min_dist[next_node] > next_dist){
							min_dist[next_node] = next_dist;
							Q.push(Data(next_node,next_dist));
						}
					}
				}
				visited_CURRY[info[Q.top().node_id].curry] = true;
			}

			Q.pop();
		}
	}

	printf("-1\n");

	return 0;
}



Measured memory_bytes: 23092