시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
4 초 512 MB 0 0 0 0.000%

문제

As one can’t make a living from the prizes awarded at computer science competitions, you have decided to get into the art business—or, more precisely, into a certain museum you have chosen for your debut as an art thief. Unfortunately, this museum is guarded quite well: there are $K$ watchmen patrolling the building, each touring on his own simple closed path through the museum.

To coordinate your operation, you use a map of the area which describes the museum as a set of $M$ corridors connecting $N$ corners, numbered from 1 to $N$. You start at corner 1, whereas your target—a valuable exhibit—is located at corner $N$. Of course, one can reach any corner of the museum from any other corner, but you are not sure whether this is possible without getting noticed, that is, without ever being at the same corner as a watchman and without passing a watchman in a corridor.

Fortunately, you got your hands on the guard roster. So you know for each watchman where he is located at the beginning and which route he is taking. Each minute, a watchman moves from his current position to the next corner on his route, and you can either stay at your current position or move to an adjacent corner. You observed that no two of the watchmen’s routes intersect and that neither your starting position nor your target is contained in any of them.

Write a program that uses this information to either calculate the minimum amount of time in minutes you need to safely reach your target* without being noticed or to decide that this is impossible.

* Once you reach the exhibit, you will open a window and leave the museum by means of the fancy wingsuit you won in your national computer science competition, so there is no need to plan a safe route back.

Of course, you are a gentleman thief who would never lay a finger on the watchmen!

입력

The first line of input contains the integers $N$ and $M$ described above. The following $M$ lines each contain two integers $u$ and $v$ ($1 \le u, v \le N$, $u \ne v$), meaning that there is a corridor directly connecting corners $u$ and $v$. It is guaranteed that at most one corridor directly connects any two given corners.

The next line contains the single integer $K$ described above. Then $K$ lines follow; the $i$th of these lines contains a sequence of integers, describing the route of the $i$th watchman as follows: The first integer $\ell_i$ gives the number of distinct corners on the route. Then $\ell_i$ pairwise distinct integers $v_1, \cdots , v_{\ell_i}$specify the sequence of corners the watchman passes on his route. More precisely, the watchman starts at corner $v_1$, in one minute he will be at $v_2$, and so on; after $\ell_i$ minutes he will be at $v_1$ again.

출력

Your program should output a single line. This should consist of an integer, the minimum amount of time in minutes you need to safely reach your target, or the string impossible if there is no way to achieve this.

제한

  • $1 \le N \le 250 000$
  • $1 \le M \le 3 000 000$
  • $3 \le \ell_i \le 1 500$
  • $\ell_1 + \cdots + \ell_K \le 2 750$

서브태스크

번호 배점 제한
1 5

$N, M \le 100~000$, $K = 1$, $\ell_1 \le 125$

2 10

$N, M \le 100~000$, $\ell_1 + \cdots + \ell_K \le 125$ and no corridor connects the routes of two distinct watchmen.

3 10

$\ell_i \le 200$, $\ell_1 + \cdots + \ell_K \le 350$ and no corridor connects the routes of two distinct watchmen.

4 10

No corridor connects the routes of two distinct watchmen.

5 25

$\ell_1 + \cdots + \ell_K \le 125$

6 20

$\ell_i \le 200$, $\ell_1 + \cdots + \ell_K \le 350$

7 20

No further constraints.

예제 입력 1

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

예제 출력 1

4

예제 입력 2

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

예제 출력 2

5

예제 입력 3

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

예제 출력 3

impossible

힌트

The following picture corresponds to the situation of the first sample case above:

Here, the corner where the watchman is located at the beginning is shown in bold and his route is indicated by shoe prints. An optimal route for you would be the following: you wait at your starting location (corner 1) for one minute, and then go to corners 2, 5, and finally 6 without further delay.

The second sample has the same museum layout, but the starting location and direction of the watchman differ. A possible optimal route: go from 1 to 2, 3, 4, 5, and finally 6.

채점 및 기타 정보

  • 예제는 채점하지 않는다.