Travel

Time limit: 1.5s Memory limit: 512MB Input: Output:

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.

Task

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

Input data

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 data

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

Constraints and clarifications

  • 1N51051 \leq N \leq 5 \cdot 10^5
  • 1M51051 \leq M \leq 5 \cdot 10^5
  • 1kiN11 \leq k_i \leq N - 1
  • 1PiN1 \leq P_i \leq N
# Score Constraints
1 10 N50N \leq 50, M50M \leq 50
2 10 N1 000N \leq 1\ 000, M1 000M \leq 1\ 000
3 20 N105N \leq 10^5, M1 000M \leq 1\ 000
4 10 N1 000N \leq 1\ 000, M105M \leq 10^5
5 15 N5105N \leq 5 \cdot 10^5, M5105M \leq 5 \cdot 10^5, Pi=1P_i = 1
6 20 N105N \leq 10^5, M105M \leq 10^5
7 15 N5105N \leq 5 \cdot 10^5, M5105M \leq 5 \cdot 10^5

Example

stdin

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

stdout

9

Explanation

Yan’s journey will take 99 days to pass through the cities 1,2,1,3,1,2,1,3,41, \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.

Log in or sign up to be able to send submissions!