#ifndef __cplusplus
    cat << EOF
#endif
#include <bits/stdc++.h>
using namespace std;

#define rep(...) overload_rep(__VA_ARGS__, rep_1, rep_0)(__VA_ARGS__)
#define rep_0(i, end) for(int i = 0; i < (end); ++i)
#define rep_1(i, fr, ba) for(int i = (fr); i <= (ba); ++i)
#define overload_rep(arg0, arg1, arg2, name, ...) name
#define all(box) box.begin(), box.end()

template <class T, class U> inline bool chmax(T& lhs, const U& rhs) { if(lhs < rhs) { lhs = rhs; return 1; } return 0; }
template <class T, class U> inline bool chmin(T& lhs, const U& rhs) { if(lhs > rhs) { lhs = rhs; return 1; } return 0; }

constexpr int inf = 1 << 30;

int X, Y, Z, N, M, S, T;
int dist[300000];
vector<int> to[300000];
vector<pair<int, int>> edges;

int bfs(int st) {
    rep(i, X + Y + Z) dist[i] = inf;
    dist[st] = 0;
    queue<int> que;
    que.push(st);
    while(!que.empty()) {
        int v = que.front();
        que.pop();
        for(int u : to[v]) {
            if(chmin(dist[u], dist[v] + 1)) que.push(u);
        }
    }
    return max(dist[edges[T].first], dist[edges[T].second]);
}

int main() {

    cin >> X >> Y >> Z >> N >> M >> S >> T;
    --S; --T; T += N;
    rep(i, N) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        b += X;
        to[a].push_back(b);
        to[b].push_back(a);
        edges.push_back(make_pair(a, b));
    }
    rep(i, M) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        a += X;
        b += X + Y;
        to[a].push_back(b);
        to[b].push_back(a);
        edges.push_back(make_pair(a, b));
    }

    int ans = min(bfs(edges[S].first), bfs(edges[S].second));
    if(ans == inf) cout << "-1\n";
    else cout << ans << '\n';
    return 0;

}

