luogu#P16390. [IATI 2024] Travel

[IATI 2024] Travel

Problem Description

Yan enjoys travelling around the country of Iatiland in his car.

Since Yan has been travelling for a long time, he has come up with a description of the map of the whole country. The cities in Iatiland are numbered from 11 to NN, and there are bi-directional direct roads connecting pairs of different cities. For each city, he has a list of cities that are reachable from it using a direct road. It is guaranteed, that there is a unique path between every pair of cities. Note that the path may not be direct, i.e. it may use several direct roads. Notice that this means that there are a total of N1N - 1 direct roads.

Yan likes travelling systematically, so he came up with an algorithm he will follow whilst going around the country.

He starts his journey in city 11 on day 11 and every subsequent day he will follow a direct road from the city he is currently in. The road he chooses is always the first road in the list of the current city. However, this simple procedure seemed pretty boring to Yan, so after picking the road he will next travel on, he removes it from the beginning of the list and appends it to the end.

Yan's main objective for travelling is to meet his friends and gossip with them. He has MM friends he wants to visit, numbered from 11 to MM and each friend ii lives in the city PiP_i. He can visit friend ii only when he is currently in city PiP_i. Additionally, Yan wants to visit his friends in the given order, i.e. he cannot visit friend i+1i+1 before visitng friend ii.

Help Yan find the minimal number of days before he visits all of his friends in the correct order.

Input Format

The first line of the standard input contains 22 numbers - NN and MM.

This is followed by NN lines, where on line ii, there are given kik_i - the number of direct paths coming out of city ii, followed by kik_i numbers - the initial list of cities to which city ii is connected by a direct path.

Following are MM numbers on one line - PiP_i, the cities where Yan's friends live.

Output Format

Output the required minimum number of days on a single line on the standard output.

4 4
2 2 3
1 1
2 1 4
1 3
2 1 1 4
9

Hint

Sample Explanation

Yan's journey will take 99 days to pass through the cities $1,\textbf{2},\textbf{1},3,\textbf{1},2,1,3,\textbf{4}$ in that sequence. He will meet friend 11 on the second day, friend 22 on the third day, friend 33 on the fifth day, and friend 44 on the ninth day.

Constraints

  • 1N5×1051 \leq N \leq 5\times 10^5
  • 1M5×1051 \leq M \leq 5\times 10^5
  • 1kiN11 \leq k_i \leq N - 1
  • 1PiN1 \leq P_i \leq N

Subtasks

Subtask Points NN MM Additional constraints
11 1010 50\leq 50 None
22 1000\leq 1000 1000\leq 1000
33 2020 105\leq 10^5
44 1010 1000\leq 1000 105\leq 10^5
55 1515 5×105\leq 5\times10^5 Pi=1P_i = 1
66 2020 105\leq 10^5 None
77 1515 5×105\leq 5\times10^5

Points for a given subtask are awarded only if all tests specified for it are successfully passed.