#include [[nodiscard]] static inline constexpr std::vector> prepare_graph(const uint_fast32_t N, [[maybe_unused]] const uint_fast32_t M, const std::vector>& edges) noexcept { std::vector> next_of(N + 1); for (const auto& [u, v] : edges) next_of[u].push_back(v), next_of[v].push_back(u); return next_of; } template [[nodiscard]] static inline constexpr std::array, MAX_K + 1> prepare_telescope([[maybe_unused]] const uint_fast32_t L, const std::vector>& telescopes) noexcept { std::array, MAX_K + 1> center_of; for (const auto& [J, K] : telescopes) center_of[MAX_K - K].push_back(J); return center_of; } template [[nodiscard]] static inline std::vector calc_safety(const uint_fast32_t N, const std::vector>& next_of, const std::array, MAX_K + 1>& center_of) noexcept { std::vector safety_of(N + 1, UINT_LEAST32_MAX); std::queue q; for (uint_fast32_t i = 0; i <= MAX_K; ++i) { while (!q.empty() && safety_of[q.front()] < i) { for (const auto& next : next_of[q.front()]) if (safety_of[next] == UINT_LEAST32_MAX) safety_of[next] = i, q.push(next); q.pop(); } for (const auto& center : center_of[i]) if (safety_of[center] == UINT_LEAST32_MAX) safety_of[center] = i, q.push(center); } return safety_of; } [[nodiscard]] static inline uint_least32_t solve(const uint_fast32_t N, const std::vector>& next_of, const std::vector& safety_of) noexcept { std::vector dist(N + 1, UINT_LEAST32_MAX); std::queue q; if (safety_of[1] == UINT_LEAST32_MAX) dist[1] = 0, q.push(1); while (!q.empty()) { for (const auto& next : next_of[q.front()]) if (safety_of[next] == UINT_LEAST32_MAX && dist[next] == UINT_LEAST32_MAX) dist[next] = dist[q.front()] + 1, q.push(next); q.pop(); } return dist[N]; } static inline void output(const uint_least32_t ans) { if (ans == UINT_LEAST32_MAX) std::cout << "No\n"; else std::cout << "Yes\n" << ans << '\n'; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); uint_fast32_t N, M, L; std::cin >> N >> M; std::vector> edges(M); for (auto& [u, v] : edges) std::cin >> u >> v; std::cin >> L; std::vector> telescopes(L); for (auto& [J, K] : telescopes) std::cin >> J >> K; const auto& next_of = prepare_graph(N, M, edges); output(solve(N, next_of, calc_safety<1000>(N, next_of, prepare_telescope<1000>(L, telescopes)))); return 0; }