Under construction

Still being built. Things may change

Music Festival

Time limit1sMemory limit1024 MB

Summary
Given albums whose tracks play in fixed order, find the album order that maximizes how many tracks are louder than every track heard before them.
Level

Medium6 of 10

Topics
Dynamic programming, Segment tree, Sorting
Solved
No attempts yet

Problem

Vitya is a boy who loves listening to music. He follows the news of his favorite bands closely, so he knows that nn albums come out this Friday, and the ii-th album has kik_i tracks. As a devoted fan, Vitya has already listened to every track that will be released soon. He knows that the coolness of the jj-th track of the ii-th album is ai,ja_{i,j}. Vitya has a friend, Masha, whom he wants to invite to a music festival where his favorite bands perform. Before she agrees, she must listen to the new releases. Each time Masha hears a track that is cooler than all the tracks before it, she gets 1 point of impression. Albums can only be played in full, and the tracks inside an album cannot be reordered. Find an order of the albums that gives Masha the largest possible impression, so that she is sure to go to the festival with him.

Input

The first line contains a single integer nn (1≤n≤2000001 \le n \le 200000), the number of albums. Each album is described by two lines. The first line contains a single integer kik_i (1≤ki≤2000001 \le k_i \le 200000), the number of tracks in the ii-th album. The next line contains kik_i integers ai,1,ai,2,ai,3,…,ai,kia_{i,1}, a_{i,2}, a_{i,3}, \ldots, a_{i,k_i} (1≤ai,j≤2000001 \le a_{i,j} \le 200000), the coolness of the tracks in the ii-th album. It is guaranteed that the sum of all kik_i does not exceed 200000.

Output

Print a single number, the maximum impression that Masha can get.

Hint

In the first sample, the optimal order is to listen to albums 4, 2, 3, and 1. In that case Masha hears the tracks in this order: 1; 7; 8, 6; 4, 9, 4, 6, 8, and gets 4 points of impression. In the second sample, you must listen to album 1 first, then album 4, and albums 2 and 3 in any order. In that case Masha gets points for every track in albums 1 and 4, and nothing for albums 2 and 3.

Examples2

  1. Example 1

    Input
    4
    5
    4 9 4 6 8
    1
    7
    2
    8 6
    1
    1
    
    Expected output
    4
    
  2. Example 2

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