Travel

시간 제한1초메모리 제한1024 MB

요약
각 도시가 떠날 때마다 인접 리스트를 회전하는 트리에서, 주어진 M개 도시를 순서대로 처음 모두 방문하는 날을 구한다.
난이도

어려움10점 중 8점

유형
트리, 시뮬레이션, 수학, 이분 탐색
정답자
아직 제출이 없습니다

문제

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 N−1N - 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 P_iP\_i. He can visit friend ii only when he is currently in city P_iP\_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.

입력

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 k_ik\_i - the number of direct paths coming out of city ii, followed by k_ik\_i numbers - the initial list of cities to which city ii is connected by a direct path.

Following are MM numbers on one line - P_iP\_i, the cities where Yan’s friends live.

출력

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

제한

  • 1≤N≤5×1051 ≤ N ≤ 5 \times 10^5
  • 1≤M≤5×1051 ≤ M ≤ 5 \times 10^5
  • 1≤k_i≤N−11 ≤ k\_i ≤ N - 1
  • 1≤P_i≤N1 ≤ P\_i ≤ N

예제1

  1. 예제 1

    입력
    4 4
    2 2 3
    1 1
    2 1 4
    1 3
    2 1 1 4
    
    예상 출력
    9