atcoder#ABC190E. [ABC190E] Magical Ornament

[ABC190E] Magical Ornament

Score : 500500 points

Problem Statement

There are NN kinds of magical gems, numbered 1,2,,N1, 2, \ldots, N, distributed in the AtCoder Kingdom. Takahashi is trying to make an ornament by arranging gems in a row. For some pairs of gems, we can put the two gems next to each other; for other pairs, we cannot. We have MM pairs for which the two gems can be adjacent: (Gem A1A_1, Gem B1B_1), (Gem A2A_2, Gem B2B_2), \ldots, (Gem AMA_M, Gem BMB_M). For the other pairs, the two gems cannot be adjacent. (Order does not matter in these pairs.) Determine whether it is possible to form a sequence of gems that has one or more gems of each of the kinds C1,C2,,CKC_1, C_2, \dots, C_K. If the answer is yes, find the minimum number of stones needed to form such a sequence.

Constraints

  • All values in input are integers.
  • 1N1051 \leq N \leq 10^5
  • 0M1050 \leq M \leq 10^5
  • 1Ai<BiN1 \leq A_i < B_i \leq N
  • If iji \neq j, (Ai,Bi)(Aj,Bj)(A_i, B_i) \neq (A_j, B_j).
  • 1K171 \leq K \leq 17
  • 1C1<C2<<CKN1 \leq C_1 < C_2 < \dots < C_K \leq N

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1

A2A_2 B2B_2

\hspace{7mm}\vdots

AMA_M BMB_M

KK

C1C_1 C2C_2 \cdots CKC_K

Output

Print the minimum number of stones needed to form a sequence of gems that has one or more gems of each of the kinds C1,C2,,CKC_1, C_2, \dots, C_K. If such a sequence cannot be formed, print -1 instead.

4 3
1 4
2 4
3 4
3
1 2 3
5

For example, by arranging the gems in the order [1,4,2,4,3][1, 4, 2, 4, 3], we can form a sequence of length 55 with Gems 1,2,31, 2, 3.

4 3
1 4
2 4
1 2
3
1 2 3
-1
10 10
3 9
3 8
8 10
2 10
5 8
6 8
5 7
6 7
1 6
2 4
4
1 2 7 9
11

For example, by arranging the gems in the order [1,6,7,5,8,3,9,3,8,10,2][1, 6, 7, 5, 8, 3, 9, 3, 8, 10, 2], we can form a sequence of length 1111 with Gems 1,2,7,91, 2, 7, 9.