#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
	int x,y,z,n,m,s,t;
	cin>>x>>y>>z>>n>>m>>s>>t;
	--s;--t;
	vector<vector<int>> v(x+y+z+n+m);
	rep(i,n){
		int a,b;
		cin>>a>>b;
		--a;--b;
		v[a].push_back(x+y+z+i);
		v[x+y+z+i].push_back(a);
		v[x+b].push_back(x+y+z+i);
		v[x+y+z+i].push_back(x+b);
	}
	rep(i,m){
		int a,b;
		cin>>a>>b;
		--a;--b;
		v[x+a].push_back(x+y+z+n+i);
		v[x+y+z+n+i].push_back(x+a);
		v[x+y+b].push_back(x+y+z+n+i);
		v[x+y+z+n+i].push_back(x+y+b);
	}
	vector<int> dist(x+y+z+n+m,inf);
	dist[x+y+z+s]=0;
	queue<int> q;
	q.push(x+y+z+s);
	while(q.size()){
		int c=q.front();q.pop();
		for(auto to:v[c]){
			if(dist[to]==inf){
				dist[to]=dist[c]+1;
				q.push(to);
			}
		}
	}
	if(dist[x+y+z+n+t]==inf)cout<<-1<<endl;
	else cout<<dist[x+y+z+n+t]/2<<endl;
	return 0;
}
