Under construction

Still being built. Things may change

From Hacks to Snitches

Time limit4sMemory limit512 MB

Summary
Find the minimum number of minutes to walk from corner 1 to corner N without meeting any of K watchmen on fixed cyclic routes, or report impossible.
Level

Hard8 of 10

Topics
Graph, BFS, Simulation
Solved
No attempts yet

Problem

Computer science competitions pay too little to make a living, so you have decided to go into the art business. More precisely, you will debut as an art thief at a museum you have chosen. Unfortunately, the museum is guarded well: KK watchmen patrol the building, each following his own simple closed path through the museum.

The museum is described by a map with NN corners and MM corridors connecting them, numbered from 1 to NN. You start at corner 1, and your target, a valuable exhibit, is at corner NN. Any corner can be reached 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 or passing a watchman in a corridor.

Fortunately, you got hold of the guard roster. For each watchman, you know where he starts and which route he takes. Each minute, a watchman moves from his current corner to the next corner on his route. You can either stay at your current corner or move to an adjacent corner. The watchmen's routes do not intersect, and neither your starting corner nor your target is on any route.

Use this information to compute the minimum number of minutes you need to reach your target safely without being noticed, or decide that this is impossible.

* Once you reach the exhibit, you will open a window and leave the museum with the wingsuit you won at your national computer science competition, so you do not need to plan a safe route back.

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

Input

The first line contains the integers NN and MM. Each of the next MM lines contains two integers uu and vv (1≤u,v≤N1 \le u, v \le N, u≠vu \ne v), meaning that a corridor directly connects corners uu and vv. At most one corridor directly connects any two corners.

The next line contains the integer KK. Then KK lines follow. The iith line describes the route of the iith watchman. The first integer ℓi\ell_i is the number of distinct corners on the route, followed by ℓi\ell_i pairwise distinct integers v1,⋯ ,vℓiv_1, \cdots, v_{\ell_i}. The watchman starts at corner v1v_1, is at v2v_2 after one minute, and so on. After ℓi\ell_i minutes he is back at v1v_1.

Output

Print a single line containing the minimum number of minutes you need to reach your target safely, or the string impossible if there is no way to do so.

Constraints

  • 1≤N≤2500001 \le N \le 250 000
  • 1≤M≤30000001 \le M \le 3 000 000
  • 3≤ℓi≤15003 \le \ell_i \le 1 500
  • ℓ1+⋯+ℓK≤2750\ell_1 + \cdots + \ell_K \le 2 750

Hint

The following picture corresponds to the situation of the first sample case above. 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 is to wait at corner 1 for one minute, then go to corners 2, 5, and finally 6 without further delay.

The second sample has the same museum layout, but the watchman's starting location and direction differ. One possible optimal route goes from 1 to 2, 3, 4, 5, and finally 6.

Examples3

  1. Example 1

    Input
    6 6
    1 2
    2 3
    3 4
    4 5
    5 2
    5 6
    1
    4 3 2 5 4
    
    Expected output
    4
    
  2. Example 2

    Input
    6 6
    1 2
    2 3
    3 4
    4 5
    5 2
    5 6
    1
    4 4 5 2 3
    
    Expected output
    5
    
  3. Example 3

    Input
    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
    
    Expected output
    impossible