#include #include using namespace std; using namespace atcoder; typedef long long ll; typedef modint998244353 mint; //typedef modint1000000007 mint; void chmin(ll& a, ll b) { a = min(a, b); } void chmax(ll& a, ll b) { a = max(a, b); } int N, M; int main() { cin >> N >> M; vector> G(N); for(int i = 0; i < M; i++){ int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } int L; cin >> L; priority_queue> Q; vector State(N, -1); for(int i = 0; i < L; i++){ int J, K; cin >> J >> K; J--; if(State[J] < K){ State[J] = K; Q.push({K, J}); } } // マルチソースBFS while(!Q.empty()){ pair now = Q.top(); Q.pop(); if(State[now.second] > 0 && State[now.second] == now.first){ for(int i = 0; i < G[now.second].size(); i++){ int neighbor = G[now.second][i]; if(State[neighbor] < State[now.second] - 1){ State[neighbor] = State[now.second] - 1; Q.push({State[neighbor], neighbor}); } } } } // BFS queue Q2; vector dist(N, INT_MAX / 2); Q2.push(0); dist[0] = 0; while(!Q2.empty()){ int now = Q2.front(); Q2.pop(); if(State[now] == -1){ for(int i = 0; i < G[now].size(); i++){ int neighbor = G[now][i]; if(State[neighbor] == -1){ if(dist[neighbor] == INT_MAX / 2){ dist[neighbor] = dist[now] + 1; Q2.push(neighbor); } } } } } if(dist[N-1] == INT_MAX / 2){ cout << "No" << endl; }else{ cout << "Yes" << endl; cout << dist[N-1] << endl; } }