#include // #include // #include // #include // #include // #include // using namespace atcoder; using namespace std; using ll = long long; using ull = unsigned long long; template using max_heap = priority_queue; template using min_heap = priority_queue, greater<>>; ll ll_min = numeric_limits::min(); ll ll_max = numeric_limits::max(); ll ALPHABET_N = 26; // using mint = modint998244353; #define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++) #define rep_(i, k, n) for (ll i = (ll)k; i < (ll)n; i++) #define all(a) a.begin(), a.end() int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll n, m; cin >> n >> m; vector> G(n); rep(i, m) { ll u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } max_heap> pq; vector bestK(n, -1); ll l; cin >> l; rep(_, l) { ll j, k; cin >> j >> k; j--; bestK[j] = k; pq.push({k, j}); } while (!pq.empty()) { auto [k, v] = pq.top(); pq.pop(); if (k < bestK[v]) continue; for (auto nex : G[v]) { if (bestK[nex] < (k - 1)) { bestK[nex] = k - 1; pq.push({k - 1, nex}); } } } vector dists(n, ll_max); if (bestK[0] == -1) { dists[0] = 0; queue q; q.push(0); while (!q.empty()) { ll v = q.front(); q.pop(); ll nd = dists[v] + 1; for (auto nex : G[v]) { if(bestK[nex] !=-1) continue; if (nd < dists[nex]) { dists[nex] = nd; q.push(nex); } } } } if (dists[n - 1] == ll_max) { cout << "No" << endl; } else { cout << "Yes" << endl; cout << dists[n - 1] << endl; } return 0; }