#include using namespace std; using ll = long long; template using vc = vector; using vi = vc; using vl = vc; template using vv = vc>; using vvi = vv; using vvl = vv; using vs = vc; using P = pair; const int INF = 1e9; const ll INF_ll = 1LL << 60; vl dx = {1, 0, -1, 0}; // vl dx = {1,1,0,-1,-1,-1,0,1}; vl dy = {0, 1, 0, -1}; // vl dx = {0,1,1,1,0,-1,-1,-1}; using ld = long double; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define drep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define drep1(i, n) for (int i = (int)(n); i >= 1; i--) #define nfor(i, s, n) for (ll i = s; i < (ll)(n); i++) // i=s,s+1...n-1 ノーマルfor #define dfor(i, s, n) for (ll i = (s)-1; i >= (ll)(n); i--) // s-1スタートでnまで落ちる #define fore(c, s) for (auto c : s) // for_each #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define YN \ { cout << "Yes" << endl; } \ else { \ cout << "No" << endl; \ } #define dame cout << -1 << endl #define vc_unique(v) v.erase(unique(v.begin(), v.end()), v.end()); bool out_grid(ll i, ll j, ll h, ll w) { // trueならcontinueする return (!(0 <= i && i < h && 0 <= j && j < w)); } ll nc2(ll x) { return x * (x - 1) / 2; } ll nc3(ll x) { return x * (x - 1) * (x - 2) / 6; } // const int mint = 998244353; // const int mint = 1000000007; int main() { int N, M; cin >> N >> M; vvi G(N); rep(i, M) { int u, v; cin >> u >> v; u--; v--; G[u].emplace_back(v); G[v].emplace_back(u); } int L; cin >> L; vi dist(N, 0); queue qu; rep(i, L) { int j, k; cin >> j >> k; j--; qu.push(j); dist[j] = k + 1; } // 幅優先探索 while (!qu.empty()) { int pos = qu.front(); qu.pop(); for (auto nx : G[pos]) { if (dist[pos] - 1 > dist[nx]) { dist[nx] = dist[pos] - 1; qu.push(nx); } } } if (dist[0] != 0) { NO; return 0; } qu.push(0); vc dist2(N, -1); dist2[0] = 0; while (!qu.empty()) { int pos = qu.front(); qu.pop(); for (auto nx : G[pos]) { if (dist[nx] != 0) continue; if (dist2[nx] != -1) continue; qu.push(nx); dist2[nx] = dist2[pos] + 1; } } if (dist2[N - 1] == -1) { NO; return 0; } YES; cout << dist2[N - 1] << endl; return 0; }