PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000020. Hòa nhạc bàn phím (Keyboards in Concert)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hòa nhạc bàn phím (Keyboards in Concert)

Keyboards in Concert

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A concert consists of mm notes to be played in their given order, using one of nn electronic keyboards. Each keyboard supports only a specified set of notes; every note in the composition can be played by at least one keyboard.

Before the first note, you may choose any keyboard for free. You may stay on the same keyboard while it supports the next note, or switch to a different one, paying one switch each time.

Determine the minimum number of keyboard switches needed to play the entire composition.

Input

The first line contains nn and mm, the number of keyboards and notes.

For each keyboard ii, the next line begins with kik_i, followed by kik_i distinct notes it supports.

The last line contains the mm notes of the composition in order.

Output

Print the minimum number of keyboard switches.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; 1≤m≤10001\le m\le 1000; 1≤ki≤10001\le k_i\le 1000; Every musical note is between 11 and 10001000; Within each keyboard the notes are distinct; each requested note can be played on at least one keyboard.
  • Subtask 2 (30 points): 1≤n≤561\le n\le 56; 1≤m≤10001\le m\le 1000; 1≤ki≤10001\le k_i\le 1000; Every musical note is between 11 and 10001000; Within each keyboard the notes are distinct; each requested note can be played on at least one keyboard.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; 1≤m≤10001\le m\le 1000; 1≤ki≤10001\le k_i\le 1000; Every musical note is between 11 and 10001000; Within each keyboard the notes are distinct; each requested note can be played on at least one keyboard.

Examples

Input

2 10
2 1 2
2 2 3
1 2 1 2 3 3 2 3 1 3

Output

3

Explanation

Keyboard 11 can play notes 1,21,2 and keyboard 22 can play notes 2,32,3. A note 11 requires keyboard 11, while note 33 requires keyboard 22. Play the first four notes (1,2,1,2)(1,2,1,2) on keyboard 11. Switch to keyboard 22 for notes 55 through 88 (3,3,2,3)(3,3,2,3), then back to keyboard 11 for note 99, and finally to keyboard 22 for note 1010. Exactly 33 switches are needed.