Under construction

Still being built. Things may change

Travel

Time limit1sMemory limit1024 MB

Summary
Yan walks a tree by always taking the first road in each city's list and moving that road to the end. Find the earliest day he visits all friends in order.
Level

Medium7 of 10

Topics
Tree, Simulation, DFS
Solved
No attempts yet

Travel

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. This means that there are a total of N−1N - 1 direct roads.

Yan likes travelling systematically, so he follows an algorithm while going around the country.

He starts his journey in city 11 on day 11. Every subsequent day he follows 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. After picking the road, he removes it from the beginning of the list and appends it to the end.

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

Find the minimal number of days before he visits all of his friends in the correct order.

Input

The first line contains two numbers NN and MM.

This is followed by NN lines. On line ii, there is 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.

The last line contains MM numbers PiP_i, the cities where Yan's friends live.

Output

Output the minimum number of days on a single line.

Constraints

  • 1≤N≤5×1051 \le N \le 5 \times 10^5
  • 1≤M≤5×1051 \le M \le 5 \times 10^5
  • 1≤ki≤N−11 \le k_i \le N - 1
  • 1≤Pi≤N1 \le P_i \le N

Examples1

  1. Example 1

    Input
    4 4
    2 2 3
    1 1
    2 1 4
    1 3
    2 1 1 4
    
    Expected output
    9