Time Travel
Berland is a country with ancient history, where roads were built and destroyed for centuries. It is known that there always were $$$n$$$ cities in Berland. You also have records of $$$t$$$ key moments in the history of the country, numbered from $$$1$$$ to $$$t$$$. Each record contains a list of bidirectional roads between some pairs of cities, which could be used for travel in Berland at a specific moment in time.You have discovered a time machine that transports you between key moments. Unfortunately, you cannot choose what point in time to end up at, but you know the order consisting of $$$k$$$ moments in time $$$a_{i}$$$, in which the machine will transport you. Since there is little time between the travels, when you find yourself in the next key moment in time (including after the last time travel), you can travel on at most one existing road at that moment, coming out from the city you were in before time travel.Currently, you are in city $$$1$$$, and the time machine has already transported you to moment $$$a_{1}$$$. You want to reach city $$$n$$$ as quickly as possible. Determine the minimum number of time travels, including the first one, that you need to make in order to reach city $$$n$$$.
In the first example, you are in city $$$1$$$ and move to moment $$$a_{1} = 2$$$. Since there are no suitable roads to pass, you do nothing and move to moment $$$a_{2} = 1$$$, after which you travel along the road $$$(1, 2)$$$. Moving to moment $$$a_{3} = 2$$$, you travel along the road $$$(2, 3)$$$. Moving to moment $$$a_{4} = 1$$$, you stay in city $$$3$$$ and make the next time travel. At time moment $$$a_{5} = 2$$$, you travel along the road $$$(3, 5)$$$ and end up in the final city after $$$5$$$ time travels.In the second example, it can be shown that it is impossible to reach city $$$5$$$ with the given time travels.
The first line contains two integers $$$n$$$ and $$$t$$$ ($$$2 \le n \le 2 \cdot 10^5, 1 \le t \le 2 \cdot 10^5$$$) — the number of cities in Berland and the number of records about key moments in history. Then follows the description of each of the $$$t$$$ records.The first line of each record description contains a single integer $$$m_{i}$$$ ($$$0 \le m_{i} \le \min \left(\frac{n(n-1)}{2}, 2 \cdot 10^5\right)$$$) — the number of roads in the $$$i$$$-th record.Each of the next $$$m_i$$$ lines contains two integers $$$v_{j}$$$ and $$$u_{j}$$$ ($$$1 \le v_{j}, u_{j} \le n$$$, $$$v_{j} \neq u_{j}$$$) — the numbers of cities connected by the $$$j$$$-th road in the $$$i$$$-th key moment in history.The next line contains a single integer $$$k$$$ ($$$1 \le k \le 2 \cdot 10^5$$$) — the number of time moments between which movements will occur.The next line contains $$$k$$$ integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_{i} \le t$$$) — the time moments at which you will be after each movement.It is guaranteed that the sum of $$$m_{i}$$$ does not exceed $$$2 \cdot 10^5$$$. It is guaranteed that each unordered pair $$$(u, v)$$$ occurs in the road description for one record no more than once.
Output a single integer — the minimum number of time travels required to reach city $$$n$$$ from city $$$1$$$, or $$$-1$$$ if it is impossible.Note that movement to time moment $$$a_{1}$$$ is also considered a movement.
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u << 20 ^ v
(n, t) = map(int, input().split())
x = [[] for _ in range(n + 1)]
c = 0
y = []
for i in range(1, t + 1):
    m = int(input())
    for _ in range(m):
        (u, v) = map(int, input().split())
        x[u].append(f(i, c))
        x[v].append(f(i, c))
        y.append(u ^ v)
        c += 1
z = [[] for _ in range(t + 1)]
visit = [0] * (n + 1)
visit[1] = 1
while x[1]:
    u = x[1].pop()
    (i, v) = (u >> 20, u & 1048575)
    z[i].append(y[v] ^ 1)
k = int(input())
a = list(map(int, input().split()))
ans = -1
for i in range(k):
    q = []
    ai = a[i]
    while z[ai]:
        u = z[ai].pop()
        if not visit[u]:
            visit[u] = 1
            q.append(u)
    for w in q:
        while x[w]:
            u = x[w].pop()
            (j, v) = (u >> 20, u & 1048575)
            z[j].append(y[v] ^ w)
    if visit[n]:
        ans = i + 1
        break
print(ans)
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u << 20 ^ v
(n, t) = map(int, input().split())
x = [[] for _ in range(n + 1)]
c = 0
y = []
for i in range(1, t + 1):
    m = int(input())
    for _ in range(m):
        (u, v) = map(int, input().split())
        x[u].append(f(i, c))
        x[v].append(f(i, c))
        y.append(u ^ v)
        c += 1
z = [[] for _ in range(t + 1)]
visit = [0] * (n + 1)
visit[1] = 1
while x[1]:
    u = x[1].pop()
    (i, v) = (u >> 20, u & 1048575)
    z[i].append(y[v] ^ 1)
k = int(input())
a = list(map(int, input().split()))
ans = -1
for i in range(k):
    q = []
    ai = a[i]
    while z[ai]:
        u = z[ai].pop()
        if not visit[u]:
            visit[u] = 1
            q.append(u)
    for w in q:
        while x[w]:
            u = x[w].pop()
            (j, v) = (u >> 20, u & 1048575)
            z[j].append(y[v] ^ w)
    if visit[n]:
        ans = i + 1
        break
print(ans)
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u << 20 ^ v
(n, t) = map(int, input().split())
x = [[] for _ in range(n + 1)]
c = 0
y = []
for i in range(1, t + 1):
    m = int(input())
    for _ in range(m):
        (u, v) = map(int, input().split())
        x[u].append(f(i, c))
        x[v].append(f(i, c))
        y.append(u ^ v)
        c += 1
z = [[] for _ in range(t + 1)]
visit = [0] * (n + 1)
visit[1] = 1
while x[1]:
    u = x[1].pop()
    (i, v) = (u >> 20, u & 1048575)
    z[i].append(y[v] ^ 1)
k = int(input())
a = list(map(int, input().split()))
ans = -1
for i in range(k):
    q = []
    ai = a[i]
    while z[ai]:
        u = z[ai].pop()
        if not visit[u]:
            visit[u] = 1
            q.append(u)
    for w in q:
        while x[w]:
            u = x[w].pop()
            (j, v) = (u >> 20, u & 1048575)
            z[j].append(y[v] ^ w)
    if visit[n]:
        ans = i + 1
        break
print(ans)