#include #include #include #include using namespace std; const int N = 1010; struct Interval { int x, y; }; int n, l [N]; queue q; int z [N], z2 [N]; Interval I [N]; vector graph [N]; bool used [N]; int binarySearch (const int &value) { int i, step; for (i = 0, step = (1 << 11); step; step >>= 1) if (i + step <= z2 [0] && z2 [i + step] < value) i = i + step; return i; } int binarySearch2 (const int &value) { int i, step; for (i = 0, step = (1 << 11); step; step >>= 1) if (i + step <= z2 [0] && z2 [i + step] <= value) i = i + step; return i; } int main () { int d, m, x, y, u = 0, kx, ky, i, j; vector :: iterator it; //freopen ("date.in", "r", stdin); scanf ("%d%d%d", &d, &n, &m); for (i = 1; i <= n; i ++) { scanf ("%d", &z [i]); } sort (z + 1, z + 1 + n); z2 [0] = 0; z [n + 1] = z [n] - 10; for (i = 1; i <= n; i ++) if (z [i] == z [i + 1]) continue; else z2 [++ z2 [0]] = z [i]; while (z2 [0] != n) z2 [0] = z2 [0]; u = 0; for (i = 1; i <= m; i ++) { scanf ("%d%d", &x, &y); if (x > y) swap (x, y); kx = binarySearch (x); kx ++; ky = binarySearch2 (y); if (kx <= ky && kx <= z2 [0] && ky <= z2 [0] && kx >= 1 && ky >= 1) { I [++ u].x = kx; I [u].y = ky; } } for (i = 1; i <= u; i ++) { for (j = 1; j <= u; j ++) { if (I [i].y + 1 == I [j].x) graph [i].push_back (j); } if (I [i].x == 1) { q.push (i); l [i] = 1; used [i] = 1; if (I [i].y == z2 [0]) { printf ("1\n"); return 0; } } } while (!q.empty ()) { kx = q.front (); q.pop (); for (it = graph [kx].begin (); it != graph [kx].end (); ++ it) if (!used [*it]) { q.push (*it); used [*it] = 1; l [*it] = l [kx] + 1; if (I [*it].y == z2 [0]) { printf ("%d\n", l [*it]); return 0; } } } return 0; }