#include #include #include #include using namespace std; constexpr int INF = 1070000000; # pragma GCC optimize("O3") int main(void){ int N, M; 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 K; cin >> K; bool A[N] = {}; for (int i = 0; i < K; i++) { int tmp; cin >> tmp; tmp--; A[tmp] = true; } int dp[N][5]; for (int i = 0; i < N; i++) for (int j = 0; j < 5; j++) dp[i][j] = INF; dp[0][0] = 0; queue> q; q.push({0, 0}); while (not q.empty()) { const array now = q.front(); q.pop(); for (int i : G[now[0]]) { const int j = A[i] ? now[1] + 1 : 0; if (j < 5 && dp[now[0]][now[1]] + 1 < dp[i][j]) { dp[i][j] = dp[now[0]][now[1]] + 1; if (i == N - 1) { cout << dp[i][j] << '\n'; return 0; } q.push({i, j}); } } } cout << -1 << '\n'; }