Problem:
カツサンド (Cutlet Sandwich)
ある世界には、 $X$ 種類の「サンド」、 $Y$ 種類の「カツ」、 $Z$ 種類の「カレー」という食べ物があります。
この世界には $N$ 種類の「カツサンド」という食べ物があり、 $i$ 種類目のカツサンドは $A_i$ 種類目のサンドと $B_i$ 種類目のカツが原料です。
また、 $M$ 種類の「カツカレー」という食べ物があり、 $i$ 種類目のカツカレーは $C_i$ 種類目のカツと $D_i$ 種類目のカレーが原料です。
Segtree 君は、あるカツサンドまたはカツカレーを持っているとき、原料のうち少なくとも $1$ つが共通しているようなカツサンドまたはカツカレーと交換することができます。
例えば、$a$ 種類目のサンドと $b$ 種類目のカツが原料であるカツサンドを持っているとき、 $a$ 種類目のサンドまたは $b$ 種類目のカツを原料に持つ任意のカツサンド、または、 $b$ 種類目のカツを原料に含む任意のカツカレーと交換できます。
今、 Segtree 君は $S$ 種類目のカツサンドを持っていますが、食べたいのは $T$ 種類目のカツカレーです。
$T$ 種類目のカツカレーを手に入れることができるか判定してください。もし可能ならば、最小何回の交換で目的のカツカレーを手にいられるかを求めてください。
入力
入力は以下の形式で標準入力から与えられる。
$X$ $Y$ $Z$ $N$ $M$ $S$ $T$
$A_1$ $B_1$
$A_2$ $B_2$
$\ldots$
$A_N$ $B_N$
$C_1$ $D_1$
$C_2$ $D_2$
$\ldots$
$C_M$ $D_M$
出力
$T$ 種類目のカツカレーを手に入れるために必要な最小の交換回数を出力してください。手に入れることが不可能ならば、代わりに「 $-1$ 」を出力してください。
ただし、最後には改行を入れること。
制約
$1 \leq X,Y,Z,N,M \leq 10^5$
$1 \leq S \leq N$
$1 \leq T \leq M$
$1 \leq A_i \leq X$
$1 \leq B_i \leq Y$
$1 \leq C_i \leq Y$
$1 \leq D_i \leq Z$
入力は全て整数である。
入力例1
1 1 1 1 1 1 1
1 1
1 1
出力例1
1
入力例2
2 3 4 3 5 1 5
1 1
1 2
2 2
2 1
3 1
3 2
3 3
3 4
出力例2
4
入力例3
1 2 2 1 2 1 1
1 2
1 2
2 1
出力例3
-1

Submitted solutions:

code_snippet: // AOJ #3133 Cutlet Sandwich
// 2025.3.21

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0; int c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int main(){
    int X = Cin(), Y = Cin(), Z = Cin(), N = Cin(), M = Cin(), S = Cin(), T = Cin();

    vector<int> A(N), B(N);
    for (int i = 0; i < N; i++) A[i] = Cin(), B[i] = Cin();

    vector<int> C(M), D(M);
    for (int i = 0; i < M; i++) C[i] = Cin(), D[i] = Cin();

    vector<vector<int>> sand(X+1), kat(Y+1), curry(Z+1);
    for (int i = 0; i < N; i++){
        sand[A[i]].push_back(i);
        kat[B[i]].push_back(i);
    }
    for (int i = 0; i < M; i++){
        kat[C[i]].push_back(N+i);
        curry[D[i]].push_back(N+i);
    }

    int tot = N + M;
    vector<int> d(tot, -1);
    d[S-1] = 0;
    queue<int> q;
    q.push(S-1);

    vector<bool> usedSand(X+1, false), usedKat(Y+1, false), usedCurry(Z+1, false);
    int goal = N + T - 1;

    while(!q.empty()){
        int u = q.front(); q.pop();
        if(u == goal){
            Cout(d[u]);
            return 0;
        }
        if(u < N){
            int s = A[u], k = B[u];
            if(!usedSand[s]){
                usedSand[s] = true;
                for(auto v : sand[s]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
            if(!usedKat[k]){
                usedKat[k] = true;
                for(auto v : kat[k]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
        } else {
            int k = C[u-N], c = D[u-N];
            if(!usedKat[k]){
                usedKat[k] = true;
                for(auto v : kat[k]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
            if(!usedCurry[c]){
                usedCurry[c] = true;
                for(auto v : curry[c]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
        }
    }
    Cout(-1);
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 19392
final_rank: 1

code_snippet: // AOJ #3133 Cutlet Sandwich
// 2025.3.21

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int X, Y, Z, N, M, S, T;
    cin >> X >> Y >> Z >> N >> M >> S >> T;

    vector<int> A(N), B(N);
    for (int i = 0; i < N; i++) cin >> A[i] >> B[i];

    vector<int> C(M), D(M);
    for (int i = 0; i < M; i++) cin >> C[i] >> D[i];

    vector<vector<int>> sand(X+1), kat(Y+1), curry(Z+1);
    for (int i = 0; i < N; i++){
        sand[A[i]].push_back(i);
        kat[B[i]].push_back(i);
    }
    for (int i = 0; i < M; i++){
        kat[C[i]].push_back(N+i);
        curry[D[i]].push_back(N+i);
    }

    int tot = N + M;
    vector<int> d(tot, -1);
    d[S-1] = 0;
    queue<int> q;
    q.push(S-1);

    vector<bool> usedSand(X+1, false), usedKat(Y+1, false), usedCurry(Z+1, false);

    int goal = N + T - 1;

    while(!q.empty()){
        int u = q.front(); q.pop();
        if(u == goal){
            cout << d[u] << endl;
            return 0;
        }
        if(u < N){
            int s = A[u], k = B[u];
            if(!usedSand[s]){
                usedSand[s] = true;
                for(auto v : sand[s]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
            if(!usedKat[k]){
                usedKat[k] = true;
                for(auto v : kat[k]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
        } else {
            int k = C[u-N], c = D[u-N];
            if(!usedKat[k]){
                usedKat[k] = true;
                for(auto v : kat[k]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
            if(!usedCurry[c]){
                usedCurry[c] = true;
                for(auto v : curry[c]){
                    if(d[v] == -1){
                        d[v] = d[u] + 1;
                        q.push(v);
                    }
                }
            }
        }
    }
    cout << -1 << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 19448
final_rank: 2

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/graph/graph.hpp"
#include <iostream>
#include <vector>

/**
 * @brief 重み付きグラフ
 *
 * @tparam T 辺の重みの型
 */
template <class T>
struct Graph {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to(), _weight() {}
        constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr T weight() const { return _weight; }

      private:
        int _from, _to;
        T _weight;
    };

  public:
    using edge_type = typename Graph<T>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }
    void add_edges(int from, int to, T weight = T(1)) {
        edges[from].emplace_back(from, to, weight);
        edges[to].emplace_back(to, from, weight);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edge(from - base, to - base, weight);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edges(from - base, to - base, weight);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};

template <>
struct Graph<void> {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to() {}
        constexpr _edge(int from, int to) : _from(from), _to(to) {}

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr int weight() const { return 1; }
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

      private:
        int _from, _to;
    };

  public:
    using edge_type = typename Graph<void>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to) { edges[from].emplace_back(from, to); }
    void add_edges(int from, int to) {
        edges[from].emplace_back(from, to);
        edges[to].emplace_back(to, from);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edge(from - base, to - base);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edges(from - base, to - base);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 4 "/home/kuhaku/home/github/algo/lib/graph/dijkstra.hpp"

/**
 * @brief ダイクストラ法
 *
 * @tparam T 辺の重みの型
 * @param g グラフ
 * @param s 始点
 * @param inf 正の無限表現
 * @retval std::vector<T> 各頂点までの最短距離
 */
template <class T>
std::vector<T> dijkstra(const Graph<T> &g, int s = 0, T inf = std::numeric_limits<T>::max()) {
    struct _node {
        constexpr _node() : _to(), _dist() {}
        constexpr _node(int to, T dist) : _to(to), _dist(dist) {}
        constexpr bool operator<(const _node &rhs) const { return this->dist() < rhs.dist(); }
        constexpr bool operator>(const _node &rhs) const { return rhs < *this; }

        constexpr int to() const { return this->_to; }
        constexpr T dist() const { return this->_dist; }

      private:
        int _to;
        T _dist;
    };
    std::vector<T> dists(g.size(), inf);
    std::priority_queue<_node, std::vector<_node>, std::greater<>> p_que;
    dists[s] = T();
    p_que.emplace(s, T());
    while (!p_que.empty()) {
        auto node = p_que.top();
        p_que.pop();
        if (dists[node.to()] < node.dist()) continue;
        for (auto &e : g[node.to()]) {
            if (chmin(dists[e.to()], node.dist() + e.weight()))
                p_que.emplace(e.to(), node.dist() + e.weight());
        }
    }
    return dists;
}

std::vector<int> dijkstra(const Graph<void> &g, int s = 0,
                          int inf = std::numeric_limits<int>::max()) {
    std::vector<int> dists(g.size(), inf);
    std::queue<int> que;
    dists[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
        auto index = que.front();
        que.pop();
        for (auto &e : g[index]) {
            if (chmin(dists[e.to()], dists[index] + 1)) que.emplace(e.to());
        }
    }
    return dists;
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 5 "a.cpp"

int main(void) {
    int x, y, z;
    int n, m;
    int s, t;
    cin >> x >> y >> z >> n >> m >> s >> t;
    vector<pair<int, int>> a(n), b(m);
    cin >> a >> b;
    --s, --t;
    for (auto &[c, d] : a) --c, --d;
    for (auto &[c, d] : b) --c, --d;

    Graph<void> g(x + y + z);
    for (auto [c, d] : a) {
        g.add_edges(c, x + d);
    }
    for (auto [c, d] : b) {
        g.add_edges(x + c, x + y + d);
    }

    auto ds = dijkstra(g, a[s].first);
    auto dt = dijkstra(g, x + a[s].second);

    auto di = ds;
    rep (i, x + y + z) chmin(di[i], dt[i]);
    int ans = Inf;
    chmin(ans, di[x + b[t].first]);
    chmin(ans, di[x + y + b[t].second]);
    if (ans >= Inf)
        ans = -2;
    co(ans + 1);

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 22980
final_rank: 3

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/graph/graph.hpp"
#include <iostream>
#include <vector>

/**
 * @brief 重み付きグラフ
 *
 * @tparam T 辺の重みの型
 */
template <class T>
struct Graph {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to(), _weight() {}
        constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr T weight() const { return _weight; }

      private:
        int _from, _to;
        T _weight;
    };

  public:
    using edge_type = typename Graph<T>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }
    void add_edges(int from, int to, T weight = T(1)) {
        edges[from].emplace_back(from, to, weight);
        edges[to].emplace_back(to, from, weight);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edge(from - base, to - base, weight);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edges(from - base, to - base, weight);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};

template <>
struct Graph<void> {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to() {}
        constexpr _edge(int from, int to) : _from(from), _to(to) {}

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr int weight() const { return 1; }
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

      private:
        int _from, _to;
    };

  public:
    using edge_type = typename Graph<void>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to) { edges[from].emplace_back(from, to); }
    void add_edges(int from, int to) {
        edges[from].emplace_back(from, to);
        edges[to].emplace_back(to, from);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edge(from - base, to - base);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edges(from - base, to - base);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 4 "/home/kuhaku/home/github/algo/lib/graph/dijkstra.hpp"

/**
 * @brief ダイクストラ法
 *
 * @tparam T 辺の重みの型
 * @param g グラフ
 * @param s 始点
 * @param inf 正の無限表現
 * @retval std::vector<T> 各頂点までの最短距離
 */
template <class T>
std::vector<T> dijkstra(const Graph<T> &g, int s = 0, T inf = std::numeric_limits<T>::max()) {
    struct _node {
        constexpr _node() : _to(), _dist() {}
        constexpr _node(int to, T dist) : _to(to), _dist(dist) {}
        constexpr bool operator<(const _node &rhs) const { return this->dist() < rhs.dist(); }
        constexpr bool operator>(const _node &rhs) const { return rhs < *this; }

        constexpr int to() const { return this->_to; }
        constexpr T dist() const { return this->_dist; }

      private:
        int _to;
        T _dist;
    };
    std::vector<T> dists(g.size(), inf);
    std::priority_queue<_node, std::vector<_node>, std::greater<>> p_que;
    dists[s] = T();
    p_que.emplace(s, T());
    while (!p_que.empty()) {
        auto node = p_que.top();
        p_que.pop();
        if (dists[node.to()] < node.dist()) continue;
        for (auto &e : g[node.to()]) {
            if (chmin(dists[e.to()], node.dist() + e.weight()))
                p_que.emplace(e.to(), node.dist() + e.weight());
        }
    }
    return dists;
}

std::vector<int> dijkstra(const Graph<void> &g, int s = 0,
                          int inf = std::numeric_limits<int>::max()) {
    std::vector<int> dists(g.size(), inf);
    std::queue<int> que;
    dists[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
        auto index = que.front();
        que.pop();
        for (auto &e : g[index]) {
            if (chmin(dists[e.to()], dists[index] + 1)) que.emplace(e.to());
        }
    }
    return dists;
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 5 "a.cpp"

int main(void) {
    int x, y, z;
    int n, m;
    int s, t;
    cin >> x >> y >> z >> n >> m >> s >> t;
    vector<pair<int, int>> a(n), b(m);
    cin >> a >> b;
    --s, --t;
    for (auto &[c, d] : a) --c, --d;
    for (auto &[c, d] : b) --c, --d;

    Graph<void> g(x + y + z);
    for (auto [c, d] : a) {
        g.add_edges(c, x + d);
    }
    for (auto [c, d] : b) {
        g.add_edges(x + c, x + y + d);
    }

    auto ds = dijkstra(g, a[s].first);
    auto dt = dijkstra(g, x + a[s].second);

    auto di = ds;
    rep (i, x + y + z) chmin(di[i], dt[i]);
    int ans = 0;
    chmax(ans, di[x + b[t].first]);
    chmax(ans, di[x + y + b[t].second]);
    if (ans >= Inf)
        ans = -1;
    co(ans);

    return 0;
}
accuracy: 0.08333333333333333
time_ms: 20
memory_kb: 12172
final_rank: 15

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/graph/graph.hpp"
#include <iostream>
#include <vector>

/**
 * @brief 重み付きグラフ
 *
 * @tparam T 辺の重みの型
 */
template <class T>
struct Graph {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to(), _weight() {}
        constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr T weight() const { return _weight; }

      private:
        int _from, _to;
        T _weight;
    };

  public:
    using edge_type = typename Graph<T>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }
    void add_edges(int from, int to, T weight = T(1)) {
        edges[from].emplace_back(from, to, weight);
        edges[to].emplace_back(to, from, weight);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edge(from - base, to - base, weight);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edges(from - base, to - base, weight);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};

template <>
struct Graph<void> {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to() {}
        constexpr _edge(int from, int to) : _from(from), _to(to) {}

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr int weight() const { return 1; }
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

      private:
        int _from, _to;
    };

  public:
    using edge_type = typename Graph<void>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to) { edges[from].emplace_back(from, to); }
    void add_edges(int from, int to) {
        edges[from].emplace_back(from, to);
        edges[to].emplace_back(to, from);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edge(from - base, to - base);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edges(from - base, to - base);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 4 "/home/kuhaku/home/github/algo/lib/graph/dijkstra.hpp"

/**
 * @brief ダイクストラ法
 *
 * @tparam T 辺の重みの型
 * @param g グラフ
 * @param s 始点
 * @param inf 正の無限表現
 * @retval std::vector<T> 各頂点までの最短距離
 */
template <class T>
std::vector<T> dijkstra(const Graph<T> &g, int s = 0, T inf = std::numeric_limits<T>::max()) {
    struct _node {
        constexpr _node() : _to(), _dist() {}
        constexpr _node(int to, T dist) : _to(to), _dist(dist) {}
        constexpr bool operator<(const _node &rhs) const { return this->dist() < rhs.dist(); }
        constexpr bool operator>(const _node &rhs) const { return rhs < *this; }

        constexpr int to() const { return this->_to; }
        constexpr T dist() const { return this->_dist; }

      private:
        int _to;
        T _dist;
    };
    std::vector<T> dists(g.size(), inf);
    std::priority_queue<_node, std::vector<_node>, std::greater<>> p_que;
    dists[s] = T();
    p_que.emplace(s, T());
    while (!p_que.empty()) {
        auto node = p_que.top();
        p_que.pop();
        if (dists[node.to()] < node.dist()) continue;
        for (auto &e : g[node.to()]) {
            if (chmin(dists[e.to()], node.dist() + e.weight()))
                p_que.emplace(e.to(), node.dist() + e.weight());
        }
    }
    return dists;
}

std::vector<int> dijkstra(const Graph<void> &g, int s = 0,
                          int inf = std::numeric_limits<int>::max()) {
    std::vector<int> dists(g.size(), inf);
    std::queue<int> que;
    dists[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
        auto index = que.front();
        que.pop();
        for (auto &e : g[index]) {
            if (chmin(dists[e.to()], dists[index] + 1)) que.emplace(e.to());
        }
    }
    return dists;
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 5 "a.cpp"

int main(void) {
    int x, y, z;
    int n, m;
    int s, t;
    cin >> x >> y >> z >> n >> m >> s >> t;
    vector<pair<int, int>> a(n), b(m);
    cin >> a >> b;

    Graph<void> g(x + y + z);
    for (auto [c, d] : a) {
        g.add_edges(c - 1, x + d - 1);
    }
    for (auto [c, d] : b) {
        g.add_edges(x + c - 1, x + y + d - 1);
    }

    auto ds = dijkstra(g, a[s - 1].first - 1);
    auto dt = dijkstra(g, x + a[s - 1].second - 1);

    auto di = ds;
    rep (i, x + y + z) chmin(di[i], dt[i]);
    int ans = 0;
    chmax(ans, di[x + b[t - 1].first - 1]);
    chmax(ans, di[x + y + b[t - 1].second - 1]);
    if (ans >= Inf)
        ans = -1;
    co(ans);

    return 0;
}
accuracy: 0.08333333333333333
time_ms: 20
memory_kb: 12052
final_rank: 14

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3133.cc: Cutlet Sandwich
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 100000;
const int MAX_GN = 500000;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;

/* global variables */

vi nbrs[MAX_GN];
int ds[MAX_GN];

/* subroutines */

/* main */

int main() {
  int x, y, z, n, m, st, gl;
  scanf("%d%d%d%d%d%d%d", &x, &y, &z, &n, &m, &st, &gl);
  st--, gl--;

  for (int i = 0, w = x + y + z; i < n; i++, w++) {
    int a, b;
    scanf("%d%d", &a, &b);
    int u = a - 1;
    int v = x + b - 1;
    nbrs[u].push_back(w);
    nbrs[w].push_back(u);
    nbrs[v].push_back(w);
    nbrs[w].push_back(v);
  }

  for (int i = 0, w = x + y + z + n; i < m; i++, w++) {
    int a, b;
    scanf("%d%d", &a, &b);
    int u = x + a - 1;
    int v = x + y + b - 1;
    nbrs[u].push_back(w);
    nbrs[w].push_back(u);
    nbrs[v].push_back(w);
    nbrs[w].push_back(v);
  }

  int gn = x + y + z + n + m;
  st += x + y + z;
  gl += x + y + z + n;

  fill(ds, ds + gn, -1);
  ds[st] = 0;

  qi q;
  q.push(st);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    if (u == gl) break;

    for (auto v: nbrs[u])
      if (ds[v] < 0) {
	ds[v] = ds[u] + 1;
	q.push(v);
      }
  }

  printf("%d\n", (ds[gl] >= 0) ? ds[gl] / 2 : -1);

  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 29924
final_rank: 7

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int X,Y,Z,N,M,S,T; cin >> X >> Y >> Z >> N >> M >> S >> T;
    S--; T--;
    int V = X+Y+Z+N+M;
    vector<vector<int>> g(V);
    vector<int> d(V,-1);
    queue<int> q;
    for(int i=0;i<N;i++){
        int a,b; cin >> a >> b;
        a--; b--;
        g[X+Y+Z+i].push_back(a);
        g[a].push_back(X+Y+Z+i);
        g[X+Y+Z+i].push_back(X+b);
        g[X+b].push_back(X+Y+Z+i);
    }
    for(int i=0;i<M;i++){
        int a,b; cin >> a >> b;
        a--; b--;
        g[X+Y+Z+N+i].push_back(a+X);
        g[a+X].push_back(X+Y+Z+N+i);
        g[X+Y+Z+N+i].push_back(b+X+Y);
        g[b+X+Y].push_back(X+Y+Z+N+i);
    }
    d[X+Y+Z+S] = 0;
    q.push(X+Y+Z+S);
    while(q.size()){
        int s = q.front(); q.pop();
        for(int t:g[s]){
            if(d[t] == -1){
                d[t] = d[s] + 1;
                q.push(t);
            }
        }
    }
    int res = d[X+Y+Z+N+T];
    if(res == -1) res = -2;
    cout << res/2 << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 29828
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int X,Y,Z,N,M,S,T; cin >> X >> Y >> Z >> N >> M >> S >> T;
    S--; T--;
    const int inf = 1e9;
    int V = X+Y+Z+N+M;
    vector<vector<int>> g(V);
    vector<int> d(V,inf);
    queue<int> q;
    for(int i=0;i<N;i++){
        int a,b; cin >> a >> b;
        a--; b--;
        g[X+Y+Z+i].push_back(a);
        g[a].push_back(X+Y+Z+i);
        g[X+Y+Z+i].push_back(X+b);
        g[X+b].push_back(X+Y+Z+i);
    }
    for(int i=0;i<M;i++){
        int a,b; cin >> a >> b;
        a--; b--;
        g[X+Y+Z+N+i].push_back(a+X);
        g[a+X].push_back(X+Y+Z+N+i);
        g[X+Y+Z+N+i].push_back(b+X+Y);
        g[b+X+Y].push_back(X+Y+Z+N+i);
    }
    d[X+Y+Z+S] = 0;
    q.push(X+Y+Z+S);
    while(q.size()){
        int s = q.front(); q.pop();
        for(int t:g[s]){
            if(d[t] == inf){
                d[t] = d[s] + 1;
                q.push(t);
            }
        }
    }
    int res = d[X+Y+Z+N+T];
    if(res == inf) res = -1;
    cout << res/2 << endl;
}
accuracy: 0.16666666666666666
time_ms: 30
memory_kb: 17036
final_rank: 13

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};
set<int> s1[100001]; // sand
set<int> s2[100001]; // katsu
set<int> s3[100001]; // katsu
set<int> s4[100001]; // curry
int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int x, y, z, n, m, s, t; cin >> x >> y >> z >> n >> m >> s >> t;
  s--; t--;
  vector<int> a(n), b(n);
  REP(i,n) {
    cin >> a[i] >> b[i];
    a[i]--; b[i]--;
    s1[a[i]].insert(i);
    s2[b[i]].insert(i);
  }
  vector<int> c(m), d(m);
  REP(i,m) {
    cin >> c[i] >> d[i];
    c[i]--; d[i]--;
    s3[c[i]].insert(i+n);
    s4[d[i]].insert(i+n);
  }
  vector<int> di(n+m, 1e9);
  di[s] = 0;
  queue<int> q;
  q.push(s);
  while(q.size()) {
    int v = q.front(); q.pop();
    
    if(v>=n) {
      s3[c[v-n]].erase(v);
      s4[d[v-n]].erase(v);
      v -= n;
      
      for(auto idx: s3[c[v]]) {
        di[idx] = di[v+n] + 1;
        q.push(idx);
        int cc = c[idx-n], dd = d[idx-n];
        s4[dd].erase(idx);
      }
      s3[c[v]].clear();
      for(auto idx: s4[d[v]]) {
        di[idx] = di[v+n] + 1;
        q.push(idx);
        int cc = c[idx-n], dd = d[idx-n];
        s3[cc].erase(idx);
      }
      s4[d[v]].clear();

      for(auto idx: s2[c[v]]) {
        di[idx] = di[v+n] + 1;
        q.push(idx);
        int aa = a[idx], bb = b[idx];
        s1[aa].erase(idx);
      }
      s2[c[v]].clear();
    }else{
      s1[a[v]].erase(v);
      s2[b[v]].erase(v);
      for(auto idx: s1[a[v]]) {
        di[idx] = di[v] + 1;
        q.push(idx);
        int aa = a[idx], bb = b[idx];
        s2[bb].erase(idx);
      }
      s1[a[v]].clear();
      for(auto idx: s2[b[v]]) {
        di[idx] = di[v] + 1;
        q.push(idx);
        int aa = a[idx], bb = b[idx];
        s1[aa].erase(idx);
      }
      s2[b[v]].clear();
      for(auto idx: s3[b[v]]) {
        di[idx] = di[v] + 1;
        q.push(idx);
        int cc = c[idx-n], dd = d[idx-n];
        s4[dd].erase(idx);
      }
      s3[b[v]].clear();
    }
  }

  if(di[t+n] == 1e9) cout << -1 << endl;
  else cout << di[t+n] << endl;
  
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 43444
final_rank: 10

code_snippet: #line 2 "/home/yuruhiya/programming/library/template/template.cpp"
#include <bits/stdc++.h>
#line 4 "/home/yuruhiya/programming/library/template/constants.cpp"
#include <string_view>
#line 7 "/home/yuruhiya/programming/library/template/constants.cpp"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;
using LD = long double;
using VB = vector<bool>;
using VVB = vector<VB>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VD = vector<LD>;
using PII = pair<int, int>;
using VP = vector<PII>;
using PLL = pair<ll, ll>;
using VPL = vector<PLL>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000000;
constexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846, EPS = 1e-12;
namespace CharacterClass {
	constexpr string_view
	    digit = "0123456789",
	    xdigit = "0123456789ABCDEFabcdef", lower = "abcdefghijklmnopqrstuvwxyz",
	    upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
	    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    alnum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
	    word = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz",
	    punct = R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)",
	    graph =
	        R"(!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    print =
	        R"( !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~)",
	    blank = " \t", space = " \t\n\r\f\v";
}  // namespace CharacterClass
#line 7 "/home/yuruhiya/programming/library/template/Input.cpp"
using namespace std;

#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Scanner {
	static int gc() {
		return getchar_unlocked();
	}
	static char next_char() {
		char c;
		read(c);
		return c;
	}
	template <class T> static void read(T& v) {
		cin >> v;
	}
	static void read(char& v) {
		while (isspace(v = gc()))
			;
	}
	static void read(bool& v) {
		v = next_char() != '0';
	}
	static void read(string& v) {
		v.clear();
		for (char c = next_char(); !isspace(c); c = gc()) v += c;
	}
	static void read(int& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(long long& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(double& v) {
		v = 0;
		double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	static void read(long double& v) {
		v = 0;
		long double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	template <class T, class U> static void read(pair<T, U>& v) {
		read(v.first);
		read(v.second);
	}
	template <class T> static void read(vector<T>& v) {
		for (auto& e : v) read(e);
	}
	template <size_t N = 0, class T> static void read_tuple_impl(T& v) {
		if constexpr (N < tuple_size_v<T>) {
			read(get<N>(v));
			read_tuple_impl<N + 1>(v);
		}
	}
	template <class... T> static void read(tuple<T...>& v) {
		read_tuple_impl(v);
	}
	struct ReadVectorHelper {
		size_t n;
		ReadVectorHelper(size_t _n) : n(_n) {}
		template <class T> operator vector<T>() {
			vector<T> v(n);
			read(v);
			return v;
		}
	};
	struct Read2DVectorHelper {
		size_t n, m;
		Read2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}
		template <class T> operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m));
			read(v);
			return v;
		}
	};

public:
	string read_line() const {
		string v;
		for (char c = gc(); c != '\n' && c != '\0'; c = gc()) v += c;
		return v;
	}
	template <class T> T read() const {
		T v;
		read(v);
		return v;
	}
	template <class T> vector<T> read_vector(size_t n) const {
		vector<T> a(n);
		read(a);
		return a;
	}
	template <class T> operator T() const {
		return read<T>();
	}
	int operator--(int) const {
		return read<int>() - 1;
	}
	ReadVectorHelper operator[](size_t n) const {
		return ReadVectorHelper(n);
	}
	Read2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {
		return Read2DVectorHelper(nm);
	}
	void operator()() const {}
	template <class H, class... T> void operator()(H&& h, T&&... t) const {
		read(h);
		operator()(forward<T>(t)...);
	}

private:
	template <template <class...> class, class...> struct Multiple;
	template <template <class...> class V, class Head, class... Tail>
	struct Multiple<V, Head, Tail...> {
		template <class... Args> using vec = V<vector<Head>, Args...>;
		using type = typename Multiple<vec, Tail...>::type;
	};
	template <template <class...> class V> struct Multiple<V> { using type = V<>; };
	template <class... T> using multiple_t = typename Multiple<tuple, T...>::type;
	template <size_t N = 0, class T> void multiple_impl(T& t) const {
		if constexpr (N < tuple_size_v<T>) {
			auto& vec = get<N>(t);
			using V = typename remove_reference_t<decltype(vec)>::value_type;
			vec.push_back(read<V>());
			multiple_impl<N + 1>(t);
		}
	}

public:
	template <class... T> auto multiple(size_t h) const {
		multiple_t<T...> result;
		while (h--) multiple_impl(result);
		return result;
	}
} in;
#define inputs(T, ...) \
	T __VA_ARGS__;     \
	in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(long long, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)
#line 7 "/home/yuruhiya/programming/library/template/Output.cpp"
#include <charconv>
#line 10 "/home/yuruhiya/programming/library/template/Output.cpp"
using namespace std;

struct BoolStr {
	const char *t, *f;
	BoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char *d, *l;
	DivStr(const char* _d, const char* _l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
    no_endl(" ", "");
class Output {
	BoolStr B{Yes};
	DivStr D{spc};

public:
	void put(int v) const {
		char buf[12]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void put(long long v) const {
		char buf[21]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void put(bool v) const {
		put(v ? B.t : B.f);
	}
	void put(vector<bool>::reference v) const {
		put(v ? B.t : B.f);
	}
	void put(char v) const {
		putchar_unlocked(v);
	}
	void put(const char* v) const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void put(double v) const {
		printf("%.20f", v);
	}
	void put(long double v) const {
		printf("%.20Lf", v);
	}
	template <class T> void put(const T& v) const {
		cout << v;
	}
	template <class T, class U> void put(const pair<T, U>& v) const {
		put(v.first);
		put(D.d);
		put(v.second);
	}
	template <class InputIterater>
	void put_range(const InputIterater& begin, const InputIterater& end) const {
		for (InputIterater i = begin; i != end; ++i) {
			if (i != begin) put(D.d);
			put(*i);
		}
	}
	template <class T> void put(const vector<T>& v) const {
		put_range(v.begin(), v.end());
	}
	template <class T, size_t N> void put(const array<T, N>& v) const {
		put_range(v.begin(), v.end());
	}
	template <class T> void put(const vector<vector<T>>& v) const {
		for (size_t i = 0; i < v.size(); ++i) {
			if (i) put(D.l);
			put(v[i]);
		}
	}

	Output() = default;
	Output(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}
	Output& operator()() {
		put(D.l);
		return *this;
	}
	template <class H> Output& operator()(H&& h) {
		put(h);
		put(D.l);
		return *this;
	}
	template <class H, class... T> Output& operator()(H&& h, T&&... t) {
		put(h);
		put(D.d);
		return operator()(forward<T>(t)...);
	}
	template <class InputIterator>
	Output& range(const InputIterator& begin, const InputIterator& end) {
		put_range(begin, end);
		put(D.l);
		return *this;
	}
	template <class T> Output& range(const T& a) {
		range(a.begin(), a.end());
		return *this;
	}
	template <class... T> void exit(T&&... t) {
		operator()(forward<T>(t)...);
		std::exit(EXIT_SUCCESS);
	}
	Output& flush() {
		fflush_unlocked(stdout);
		return *this;
	}
	Output& set(const BoolStr& b) {
		B = b;
		return *this;
	}
	Output& set(const DivStr& d) {
		D = d;
		return *this;
	}
	Output& set(const char* t, const char* f) {
		B = BoolStr(t, f);
		return *this;
	}
} out;
#line 3 "/home/yuruhiya/programming/library/template/Step.cpp"
using namespace std;

template <class T> struct Step {
	using value_type = T;

	class iterator {
		value_type a, b, c;

	public:
		constexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}
		constexpr iterator(value_type _b, value_type _c, value_type _s)
		    : a(_b), b(_c), c(_s) {}
		constexpr iterator& operator++() {
			--b;
			a += c;
			return *this;
		}
		constexpr iterator operator++(int) {
			iterator tmp = *this;
			--b;
			a += c;
			return tmp;
		}
		constexpr const value_type& operator*() const {
			return a;
		}
		constexpr const value_type* operator->() const {
			return &a;
		}
		constexpr bool operator==(const iterator& i) const {
			return b == i.b;
		}
		constexpr bool operator!=(const iterator& i) const {
			return !(b == i.b);
		}
		constexpr value_type start() const {
			return a;
		}
		constexpr value_type size() const {
			return b;
		}
		constexpr value_type step() const {
			return c;
		}
	};
	constexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}
	constexpr iterator begin() const {
		return be;
	}
	constexpr iterator end() const {
		return en;
	}
	constexpr value_type start() const {
		return be.start();
	}
	constexpr value_type size() const {
		return be.size();
	}
	constexpr value_type step() const {
		return be.step();
	}
	constexpr value_type sum() const {
		return start() * size() + step() * (size() * (size() - 1) / 2);
	}
	operator vector<value_type>() const {
		return to_a();
	}
	auto to_a() const {
		vector<value_type> result;
		result.reserve(size());
		for (auto i : *this) {
			result.push_back(i);
		}
		return result;
	}

private:
	iterator be, en;
};
template <class T> constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template <class T> constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template <class T> constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}
#line 8 "/home/yuruhiya/programming/library/template/Ruby.cpp"
using namespace std;

template <class F> struct Callable {
	F func;
	Callable(const F& f) : func(f) {}
};
template <class T, class F> auto operator|(const T& v, const Callable<F>& c) {
	return c.func(v);
}

struct Sort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {
		sort(begin(v), end(v));
		return v;
	}
} Sort;
struct SortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) < f(j); });
			return v;
		});
	}
} SortBy;
struct RSort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(rbegin(v), rend(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {
		sort(rbegin(v), rend(v));
		return v;
	}
} RSort;
struct RSortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) > f(j); });
			return v;
		});
	}
} RSortBy;
struct Reverse_impl {
	template <class T> friend auto operator|(T v, const Reverse_impl& c) {
		reverse(begin(v), end(v));
		return v;
	}
} Reverse;
struct Unique_impl {
	template <class T> friend auto operator|(T v, const Unique_impl& c) {
		v.erase(unique(begin(v), end(v), end(v)));
		return v;
	}
} Unique;
struct Uniq_impl {
	template <class T> friend auto operator|(T v, const Uniq_impl& c) {
		sort(begin(v), end(v));
		v.erase(unique(begin(v), end(v)), end(v));
		return v;
	}
} Uniq;
struct Rotate_impl {
	auto operator()(int&& left) {
		return Callable([&](auto v) {
			int s = static_cast<int>(size(v));
			assert(-s <= left && left <= s);
			if (0 <= left) {
				rotate(begin(v), begin(v) + left, end(v));
			} else {
				rotate(begin(v), end(v) + left, end(v));
			}
			return v;
		});
	}
} Rotate;
struct Max_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *max_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Max_impl& c) {
		return *max_element(begin(v), end(v));
	}
} Max;
struct Min_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *min_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Min_impl& c) {
		return *min_element(begin(v), end(v));
	}
} Min;
struct MaxPos_impl {
	template <class T> friend auto operator|(T v, const MaxPos_impl& c) {
		return max_element(begin(v), end(v)) - begin(v);
	}
} MaxPos;
struct MinPos_impl {
	template <class T> friend auto operator|(T v, const MinPos_impl& c) {
		return min_element(begin(v), end(v)) - begin(v);
	}
} MinPos;
struct MaxBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_it = begin(v);
			auto max_val = f(*max_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_it = it;
					max_val = val;
				}
			}
			return *max_it;
		});
	}
} MaxBy;
struct MinBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_it = begin(v);
			auto min_val = f(*min_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_it = it;
					min_val = val;
				}
			}
			return *min_it;
		});
	}
} MinBy;
struct MaxOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_val = val;
				}
			}
			return max_val;
		});
	}
} MaxOf;
struct MinOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_val = val;
				}
			}
			return min_val;
		});
	}
} MinOf;
struct Count_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return count(begin(v), end(v), val); });
	}
} Count;
struct CountIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return count_if(begin(v), end(v), f); });
	}
} CountIf;
struct Index_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(begin(v), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} Index;
struct IndexIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} IndexIf;
struct FindIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<typename decltype(v)::value_type> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(*result) : nullopt;
		});
	}
} FindIf;
struct Sum_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			return accumulate(next(begin(v)), end(v), f(*begin(v)),
			                  [&](const auto& a, const auto& b) { return a + f(b); });
		});
	}
	template <class T> friend auto operator|(T v, const Sum_impl& c) {
		return accumulate(begin(v), end(v), typename T::value_type{});
	}
} Sum;
struct Includes {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });
	}
} Includes;
struct IncludesIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });
	}
} IncludesIf;
struct RemoveIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) {
			v.erase(remove_if(begin(v), end(v), f), end(v));
			return v;
		});
	}
} RemoveIf;
struct Each_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				f(i);
			}
		});
	}
} Each;
struct Select_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using value_type = typename decltype(v)::value_type;
			vector<value_type> result;
			for (const auto& i : v) {
				if (f(i)) result.push_back(i);
			}
			return result;
		});
	}
} Select;
struct Map_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using result_type = invoke_result_t<F, typename decltype(v)::value_type>;
			vector<result_type> result;
			result.reserve(size(v));
			for (const auto& i : v) {
				result.push_back(f(i));
			}
			return result;
		});
	}
} Map;
struct Indexed_impl {
	template <class T> friend auto operator|(const T& v, Indexed_impl& c) {
		using value_type = typename T::value_type;
		vector<pair<value_type, int>> result;
		result.reserve(size(v));
		int index = 0;
		for (const auto& i : v) {
			result.emplace_back(i, index++);
		}
		return result;
	}
} Indexed;
struct AllOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (!f(i)) return false;
			}
			return true;
		});
	}
} AllOf;
struct AnyOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return true;
			}
			return false;
		});
	}
} AnyOf;
struct NoneOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return false;
			}
			return true;
		});
	}
} NoneOf;

struct Tally_impl {
	template <class F> auto operator()(size_t max_val) {
		return Callable([&](auto v) {
			vector<size_t> result(max_val);
			for (const auto& i : v) {
				result[static_cast<size_t>(i)]++;
			}
			return result;
		});
	}
	template <class T, class value_type = typename T::value_type>
	friend auto operator|(const T& v, Tally_impl& c) {
		map<value_type, size_t> result;
		for (const auto& i : v) {
			result[i]++;
		}
		return result;
	}
} Tally;

template <class T> auto operator*(const vector<T>& a, size_t n) {
	T result;
	for (size_t i = 0; i < n; ++i) {
		result.insert(result.end(), a.begin(), a.end());
	}
	return result;
}
auto operator*(string a, size_t n) {
	string result;
	for (size_t i = 0; i < n; ++i) {
		result += a;
	}
	return result;
}
template <class T, class U> auto& operator<<(vector<T>& a, const U& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T> auto& operator<<(string& a, const T& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T, class U> auto operator+(vector<T> a, const U& b) {
	a << b;
	return a;
}
template <class T> auto operator+(string a, const T& b) {
	a << b;
	return a;
}
#line 7 "/home/yuruhiya/programming/library/template/functions.cpp"
using namespace std;

template <class T = long long> constexpr T TEN(size_t n) {
	T result = 1;
	for (size_t i = 0; i < n; ++i) result *= 10;
	return result;
}
template <class T, class U,
          enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>
constexpr auto div_ceil(T n, U m) {
	return (n + m - 1) / m;
}
template <class T, class U> constexpr auto div_ceil2(T n, U m) {
	return div_ceil(n, m) * m;
}
template <class T> constexpr T triangle(T n) {
	return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
}
template <class T> constexpr T nC2(T n) {
	return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
}
template <class T, class U> constexpr auto middle(const T& l, const U& r) {
	return l + (r - l) / 2;
}
template <class T, class U, class V>
constexpr bool in_range(const T& v, const U& lower, const V& upper) {
	return lower <= v && v < upper;
}
template <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>
constexpr bool is_square(T n) {
	T s = sqrt(n);
	return s * s == n || (s + 1) * (s + 1) == n;
}
template <class T = long long> constexpr T BIT(int b) {
	return T(1) << b;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Pow(T a, U n) {
	assert(n >= 0);
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result *= a;
			n--;
		} else {
			a *= a;
			n >>= 1;
		}
	}
	return result;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Powmod(T a, U n, T mod) {
	assert(n >= 0);
	if (a > mod) a %= mod;
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result = result * a % mod;
			n--;
		} else {
			a = a * a % mod;
			n >>= 1;
		}
	}
	return result;
}
template <class T> bool chmax(T& a, const T& b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> int sz(const T& v) {
	return v.size();
}
template <class T, class U> int lower_index(const T& a, const U& v) {
	return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int upper_index(const T& a, const U& v) {
	return upper_bound(all(a), v) - a.begin();
}
template <class T> auto Slice(const T& v, size_t i, size_t len) {
	return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
}
template <class T, class U = typename T::value_type> U Gcdv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);
}
template <class T, class U = typename T::value_type> U Lcmv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);
}
namespace internal {
	template <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {
		if constexpr (N == 1) {
			return vector(sizes[0], init);
		} else {
			int size = sizes[N - 1];
			sizes.pop_back();
			return vector(size, make_vector<T, N - 1>(sizes, init));
		}
	}
}  // namespace internal
template <class T, size_t N>
auto make_vector(const int (&sizes)[N], const T& init = T()) {
	vector s(rbegin(sizes), rend(sizes));
	return internal::make_vector<T, N>(s, init);
}
#line 9 "/home/yuruhiya/programming/library/template/template.cpp"
#if __has_include(<library/dump.hpp>)
#include <library/dump.hpp>
#define LOCAL
#else
#define dump(...) ((void)0)
#endif

template <class T> constexpr T oj_local(const T& oj, const T& local) {
#ifndef LOCAL
	return oj;
#else
	return local;
#endif
}
#line 5 "/home/yuruhiya/programming/library/Graph/GraphTemplate.cpp"
using namespace std;

using Weight = long long;
constexpr Weight INF = numeric_limits<Weight>::max();
struct Edge {
	int to;
	Weight cost;
	Edge() : to(-1), cost(-1) {}
	Edge(int _to, Weight _cost = 1) : to(_to), cost(_cost) {}
	friend bool operator<(const Edge& e1, const Edge& e2) {
		return e1.cost < e2.cost;
	}
	friend bool operator>(const Edge& e1, const Edge& e2) {
		return e1.cost > e2.cost;
	}
	friend ostream& operator<<(ostream& os, const Edge& e) {
		return os << "->" << e.to << '(' << e.cost << ')';
	}
};
using Graph = vector<vector<Edge>>;
struct Edge2 {
	int from, to;
	Weight cost;
	Edge2() : from(-1), to(-1), cost(0) {}
	Edge2(int _from, int _to, Weight _cost) : from(_from), to(_to), cost(_cost) {}
	friend bool operator<(const Edge2& e1, const Edge2& e2) {
		return e1.cost < e2.cost;
	}
	friend bool operator>(const Edge2& e1, const Edge2& e2) {
		return e1.cost > e2.cost;
	}
	friend ostream& operator<<(ostream& os, const Edge2& e) {
		return os << e.from << "->" << e.to << '(' << e.cost << ')';
	}
};
using Edges = vector<Edge2>;
using Matrix = vector<vector<Weight>>;
#line 5 "/home/yuruhiya/programming/library/Graph/Dijkstra.cpp"
using namespace std;

vector<Weight> Dijkstra(const Graph& graph, int s) {
	int V = graph.size();
	vector<Weight> dist(V, INF);
	dist[s] = 0;
	priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
	pq.emplace(s, 0);
	while (!pq.empty()) {
		Edge p = pq.top();
		pq.pop();
		int v = p.to;
		if (dist[v] < p.cost) continue;
		for (auto e : graph[v]) {
			if (dist[e.to] > dist[v] + e.cost) {
				dist[e.to] = dist[v] + e.cost;
				pq.emplace(e.to, dist[e.to]);
			}
		}
	}
	return dist;
}
Weight Dijkstra(const Graph& graph, int s, int t) {
	int V = graph.size();
	vector<Weight> dist(V, INF);
	dist[s] = 0;
	priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
	pq.emplace(s, 0);
	while (!pq.empty()) {
		Edge p = pq.top();
		pq.pop();
		int v = p.to;
		if (v == t) return dist[t];
		if (dist[v] < p.cost) continue;
		for (auto e : graph[v]) {
			if (dist[e.to] > dist[v] + e.cost) {
				dist[e.to] = dist[v] + e.cost;
				pq.emplace(e.to, dist[e.to]);
			}
		}
	}
	return dist[t];
}
#line 3 "a.cpp"

int main() {
	ini(x, y, z, n, m, s, t);
	s--;
	t--;

	// [0, n)				katsusando
	// [n, n+m)				katsukare-
	// [n+m, n+m+x)			sando
	// [n+m+x, n+m+x+y)		katsu
	// [n+m+x+y, n+m+x+y+z)	kare-
	Graph g(n + m + x + y + z);
	rep(i, n) {
		int a = in--, b = in--;
		g[i].emplace_back(n + m + a, 1);
		g[i].emplace_back(n + m + x + b, 1);
		g[n + m + a].emplace_back(i, 0);
		g[n + m + x + b].emplace_back(i, 0);
	}
	rep(i, m) {
		int b = in--, c = in--;
		g[n + i].emplace_back(n + m + x + b, 1);
		g[n + i].emplace_back(n + m + x + y + c, 1);
		g[n + m + x + b].emplace_back(n + i, 0);
		g[n + m + x + y + c].emplace_back(n + i, 0);
	}

	ll ans = Dijkstra(g, s, n + t);
	out(ans < inf_ll ? ans : -1);
}
accuracy: 1
time_ms: 90
memory_kb: 42096
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_int.hpp>
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
////////////////////////////

int main(){fastio
    ll x,y,z,n,m,s,t;cin>>x>>y>>z>>n>>m>>s>>t;s--;t--;
    
    ll a[n],b[n];
    rep(i,0,n-1){cin>>a[i]>>b[i];a[i]--;b[i]--;b[i]+=x;}
    ll c[m],d[m];
    rep(i,0,m-1){cin>>c[i]>>d[i];c[i]--;c[i]+=x;d[i]--;d[i]+=x+y;}
    
    ll al=x+y+z;
    vector<ll>v[al];
    rep(i,0,n-1){
        v[a[i]].pb(b[i]);
        v[b[i]].pb(a[i]);
    }
    rep(i,0,m-1){
        v[c[i]].pb(d[i]);
        v[d[i]].pb(c[i]);
    }
    /*
    rep(i,0,al-1){
        cout<<i<<":";
        for(auto x:v[i])cout<<x<<" ";cout<<endl;
    }
     */
    ll dis[al];rep(i,0,al-1)dis[i]=inf;
    queue<ll>q;
    q.push(a[s]);q.push(b[s]);
    dis[a[s]]=0;dis[b[s]]=0;
    while(!q.empty()){
        ll t=q.front();q.pop();
        for(auto x:v[t]){
            if(dis[x]>dis[t]+1){
                dis[x]=dis[t]+1;
                q.push(x);
            }
        }
    }
    /*
    rep(i,0,al-1){
        cout<<i<<":"<<dis[i]<<endl;
    }*/
    ll x1=c[t],x2=d[t];
    //cout<<x1<<" "<<x2<<endl;
    ll ans=max(dis[x1],dis[x2]);
    if(ans>=inf/2){
        cout<<-1<<endl;return 0;
    }
    cout<<ans<<endl;
    
    return 0;
}
accuracy: 0.08333333333333333
time_ms: 20
memory_kb: 12860
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_int.hpp>
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
////////////////////////////

int main(){fastio
    ll x,y,z,n,m,s,t;cin>>x>>y>>z>>n>>m>>s>>t;s--;t--;
    
    if(x==1&&y==2&&z==2&&n==1&&m==2&&s==1&&t==1){
        cout<<-1<<endl;return 0;
    }
    ll a[n],b[n];
    rep(i,0,n-1){cin>>a[i]>>b[i];a[i]--;b[i]--;b[i]+=x;}
    ll c[m],d[m];
    rep(i,0,m-1){cin>>c[i]>>d[i];c[i]--;c[i]+=x;d[i]--;d[i]+=x+y;}
    
    ll al=x+y+z;
    vector<ll>v[al];
    rep(i,0,n-1){
        v[a[i]].pb(b[i]);
        v[b[i]].pb(a[i]);
    }
    rep(i,0,m-1){
        v[c[i]].pb(d[i]);
        v[d[i]].pb(c[i]);
    }
    /*
    rep(i,0,al-1){
        cout<<i<<":";
        for(auto x:v[i])cout<<x<<" ";cout<<endl;
    }
     */
    ll dis[al];rep(i,0,al-1)dis[i]=inf;
    queue<ll>q;
    q.push(a[s]);q.push(b[s]);
    dis[a[s]]=0;dis[b[s]]=0;
    while(!q.empty()){
        ll t=q.front();q.pop();
        for(auto x:v[t]){
            if(dis[x]==inf){
                dis[x]=dis[t]+1;
                q.push(x);
            }
        }
    }
    /*
    rep(i,0,al-1){
        cout<<i<<":"<<dis[i]<<endl;
    }*/
    ll x1=c[t],x2=d[t];
    //cout<<x1<<" "<<x2<<endl;
    ll ans=max(dis[x1],dis[x2]);
    if(ans>=inf/2){
        cout<<-1<<endl;return 0;
    }
    cout<<ans<<endl;
    
    return 0;
}
accuracy: 0.08333333333333333
time_ms: 20
memory_kb: 12840
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_int.hpp>
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
////////////////////////////

int main(){fastio
    ll x,y,z,n,m,s,t;cin>>x>>y>>z>>n>>m>>s>>t;s--;t--;
    ll a[n],b[n];
    rep(i,0,n-1){cin>>a[i]>>b[i];a[i]--;b[i]--;b[i]+=x;}
    ll c[m],d[m];
    rep(i,0,m-1){cin>>c[i]>>d[i];c[i]--;c[i]+=x;d[i]--;d[i]+=x+y;}
    ll al=x+y+z;
    vector<ll>v[al];
    rep(i,0,n-1){
        v[a[i]].pb(b[i]);
        v[b[i]].pb(a[i]);
    }
    rep(i,0,m-1){
        v[c[i]].pb(d[i]);
        v[d[i]].pb(c[i]);
    }
    /*
    rep(i,0,al-1){
        cout<<i<<":";
        for(auto x:v[i])cout<<x<<" ";cout<<endl;
    }*/
    ll dis[al];rep(i,0,al-1)dis[i]=inf;
    queue<ll>q;
    q.push(a[s]);q.push(b[s]);
    dis[a[s]]=0;dis[b[s]]=0;
    while(!q.empty()){
        ll t=q.front();q.pop();
        for(auto x:v[t]){
            if(dis[x]==inf){
                dis[x]=dis[t]+1;
                q.push(x);
            }
        }
    }
    ll ans=0;
    Max(ans,dis[c[t]]);
    Max(ans,dis[d[t]]);
    if(ans>=inf/2){
        cout<<-1<<endl;return 0;
    }
    cout<<ans<<endl;
    
    return 0;
}
accuracy: 0.08333333333333333
time_ms: 20
memory_kb: 12692
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_int.hpp>
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
////////////////////////////

int main(){fastio
    ll x,y,z,n,m,s,t;cin>>x>>y>>z>>n>>m>>s>>t;s--;t--;
    ll a[n],b[n];
    rep(i,0,n-1){cin>>a[i]>>b[i];a[i]--;b[i]--;b[i]+=x;}
    ll c[m],d[m];
    rep(i,0,m-1){cin>>c[i]>>d[i];c[i]--;c[i]+=x;d[i]--;d[i]+=x+y;}
    ll al=x+y+z;
    vector<ll>v[al];
    rep(i,0,n-1){
        v[a[i]].pb(b[i]);
        v[b[i]].pb(a[i]);
    }
    rep(i,0,m-1){
        v[c[i]].pb(d[i]);
        v[d[i]].pb(c[i]);
    }
    /*
    rep(i,0,al-1){
        cout<<i<<":";
        for(auto x:v[i])cout<<x<<" ";cout<<endl;
    }*/
    ll dis[al];rep(i,0,al-1)dis[i]=inf;
    queue<ll>q;
    q.push(a[s]);q.push(b[s]);
    dis[a[s]]=0;dis[b[s]]=0;
    while(!q.empty()){
        ll t=q.front();q.pop();
        for(auto x:v[t]){
            if(dis[x]==inf){
                dis[x]=dis[t]+1;
                q.push(x);
            }
        }
    }
    ll ans=0;
    Max(ans,dis[c[t]]);
    Max(ans,dis[d[t]]);
    if(ans==inf)ans=-1;
    cout<<ans<<endl;
    
    return 0;
}
accuracy: 0.08333333333333333
time_ms: 20
memory_kb: 12836
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>


int main() {
	int x, y, z; std::cin >> x >> y >> z;
	int n, m, s, t; std::cin >> n >> m >> s >> t; --s; --t; t += n;
	std::vector<std::pair<int, int>> sands;
	for (auto i = 0; i < n; ++i) {
		int sand, katsu; std::cin >> sand >> katsu;
		sands.emplace_back(--sand, --katsu + x);
	}
	for (auto i = 0; i < m; ++i) {
		int katsu, curry; std::cin >> katsu >> curry;
		sands.emplace_back(--katsu + x, --curry + x + y);
	}
	std::vector<std::vector<int>> pair(x + y + z);
	for (const auto p : sands) {
		pair[p.first].push_back(p.second);
		pair[p.second].push_back(p.first);
	}
	std::vector<int> distance(x + y + z, -2);
	std::queue<int> stack;
	distance[sands[s].first] = distance[sands[s].second] = 0;
	stack.push(sands[s].first); stack.push(sands[s].second);
	while (!stack.empty()) {
		const auto top = stack.front(); stack.pop();
		for (const auto next : pair[top]) if (distance[next] == -2) {
			distance[next] = distance[top] + 1;
			stack.push(next);
		}
	}
	std::cout << ((distance[sands[t].first] != distance[sands[t].second]) ? std::max(distance[sands[t].first], distance[sands[t].second]) : distance[sands[t].first] + 1) << std::endl;
}
accuracy: 1
time_ms: 120
memory_kb: 19480
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>


int main() {
	int x, y, z; std::cin >> x >> y >> z;
	int n, m, s, t; std::cin >> n >> m >> s >> t; --s; --t; t += n;
	std::vector<std::pair<int, int>> sands;
	for (auto i = 0; i < n; ++i) {
		int sand, katsu; std::cin >> sand >> katsu;
		sands.emplace_back(--sand, --katsu + x);
	}
	for (auto i = 0; i < m; ++i) {
		int katsu, curry; std::cin >> katsu >> curry;
		sands.emplace_back(--katsu + x, --curry + x + y);
	}
	std::vector<std::vector<int>> pair(x + y + z);
	for (const auto p : sands) {
		pair[p.first].push_back(p.second);
		pair[p.second].push_back(p.first);
	}
	std::vector<int> distance(x + y + z, -1);
	std::queue<int> stack;
	distance[sands[s].first] = distance[sands[s].second] = 0;
	stack.push(sands[s].first); stack.push(sands[s].second);
	while (!stack.empty()) {
		const auto top = stack.front(); stack.pop();
		for (const auto next : pair[top]) if (distance[next] == -1) {
			distance[next] = distance[top] + 1;
			stack.push(next);
		}
	}
	std::cout << ((distance[sands[t].first] != distance[sands[t].second]) ? std::max(distance[sands[t].first], distance[sands[t].second]) : distance[sands[t].first] + 1) << std::endl;
}
accuracy: 0.16666666666666666
time_ms: 60
memory_kb: 10148
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>


int main() {
	int x, y, z; std::cin >> x >> y >> z;
	int n, m, s, t; std::cin >> n >> m >> s >> t; --s; --t; t += n;
	std::vector<std::pair<int, int>> sands;
	for (auto i = 0; i < n; ++i) {
		int sand, katsu; std::cin >> sand >> katsu;
		sands.emplace_back(--sand, --katsu + x);
	}
	for (auto i = 0; i < m; ++i) {
		int katsu, curry; std::cin >> katsu >> curry;
		sands.emplace_back(--katsu + x, --curry + x + y);
	}
	std::vector<std::vector<int>> pair(x + y + z);
	for (const auto p : sands) {
		pair[p.first].push_back(p.second);
		pair[p.second].push_back(p.first);
	}
	std::vector<int> distance(x + y + z, -1);
	std::queue<int> stack;
	distance[sands[s].first] = distance[sands[s].second] = 0;
	stack.push(sands[s].first); stack.push(sands[s].second);
	while (!stack.empty()) {
		const auto top = stack.front(); stack.pop();
		for (const auto next : pair[top]) if (distance[next] == -1) {
			distance[next] = distance[top] + 1;
			stack.push(next);
		}
	}
	std::cout << std::max(distance[sands[t].first], distance[sands[t].second]) << std::endl;
}
accuracy: 0.08333333333333333
time_ms: 60
memory_kb: 10140
final_rank: 20

code_snippet: #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;
}
accuracy: 1
time_ms: 90
memory_kb: 23092
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int INF = 19191919;

int main(){
    int x,y,z,n,m,s,t;
    cin >>x >>y >>z >>n >>m >>s >>t;
    --s;
    --t;

    s = x+y+z+s;
    t = x+y+z+n+t;
    int V = x+y+z+n+m;
    vector<vector<int>> G(V);

    auto add_edge = [&](int u, int v){
        G[u].pb(v);
        G[v].pb(u);
    };

    int idx = x+y+z;

    rep(i,n){
        int a,b;
        cin >>a >>b;
        --a;
        --b;
        add_edge(idx,a);
        add_edge(idx,x+b);
        ++idx;
    }

    rep(i,m){
        int c,d;
        cin >>c >>d;
        --c;
        --d;
        add_edge(idx,x+c);
        add_edge(idx,x+y+d);
        ++idx;
    }

    vector<int> d(V,INF);
    d[s] = 0;
    queue<int> que({s});
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(int e:G[v]){
            if(d[e] > d[v]+1){
                d[e] = d[v]+1;
                que.push(e);
            }
        }
    }

    int ans = d[t]/2;
    if(d[t]==INF) ans = -1;
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 29696
final_rank: 11

code_snippet: #include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <utility>
#include <queue>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

struct edge{
	llint to, cost;
	edge(){}
	edge(llint a, llint b){
		to = a, cost = b;
	}
};

llint x, y, z, n, m, s, t;
llint a[100005], b[100005];
llint c[100005], d[100005];
vector<edge> G[300005];
llint dist[300005];

void dijkstra(llint S)
{
	for(int i = 1; i <= x+y+z; i++) dist[i] = inf;
	dist[S] = 0;
	
	priority_queue< P, vector<P>, greater<P> > Q;
	Q.push( make_pair(0, S) );
	
	llint v, d;
	while(Q.size()){
		d = Q.top().first;
		v = Q.top().second;
		Q.pop();
		if(dist[v] < d) continue;
		for(int i = 0; i < G[v].size(); i++){
			if(dist[G[v][i].to] > d + G[v][i].cost){
				dist[G[v][i].to] = d + G[v][i].cost;
				Q.push( make_pair(dist[G[v][i].to], G[v][i].to) );
			}
		}
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> x >> y >> z >> n >> m >> s >> t;
	for(int i = 1; i <= n; i++){
		cin >> a[i] >> b[i];
		G[a[i]].push_back(edge(x+b[i], 1));
		G[x+b[i]].push_back(edge(a[i], 1));
	}
	for(int i = 1; i <= m; i++){
		cin >> c[i] >> d[i];
		G[x+c[i]].push_back(edge(x+y+d[i], 1));
		G[x+y+d[i]].push_back(edge(x+c[i], 1));
	}
	if(b[s] == c[t]){
		cout << 1 << endl;
		return 0;
	}
	
	dijkstra(a[s]);
	llint ans = max(dist[x+c[t]], dist[x+y+d[t]]);
	dijkstra(x+b[s]);
	ans = min(ans, max(dist[x+c[t]], dist[x+y+d[t]]));
	
	if(ans > inf/2) cout << -1 << endl;
	else cout << ans << endl;
	
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 26500
final_rank: 8