Under construction

Still being built. Things may change

Ornithology

Interview

Time limit5sMemory limit1024 MB

Summary
Count the pairs of bird segments from the first line to the second line that cross, given each start position's desired targets.
Level

Medium4 of 10

Topics
Array, Prefix sum
Solved
No attempts yet

Problem

On the outskirts of town, close to a farm, two parallel power lines run with a narrow dirt road between them. The lines are a favorite resting spot for many birds.

On this cool autumn morning, a flock of birds sits on the lines. The first line has nn consecutive positions for birds, numbered 0,1,…,n−10, 1, \dots, n-1. The second line also has nn positions numbered the same way.

At the start, some positions on the first line hold birds (a position may hold zero birds), and the second line is empty. Each bird has a desired position on the second line. Birds that start from the same position have different desired positions.

At one moment, all birds fly to their desired positions at once. Each bird flies along the straight segment connecting its start and desired position.

If two segments cross, the two birds may collide. An unordered pair of birds like this is a dangerous pair.

For example, if the bird on position 2 wants to go to position 1 and the bird on position 1 wants to go to position 2, their paths cross.

Pairs of birds that start from the same position or want the same desired position are not dangerous pairs.

Count the dangerous pairs.

Input

The first line contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5), the number of positions on each power line.

Each of the next nn lines describes the desired positions of the birds on one starting position. The ii-th line starts with an integer pip_i (0≤pi≤n0 \le p_i \le n), the number of birds on position ii. Then come pip_i distinct integers qi,1,…,qi,piq_{i,1}, \dots, q_{i,p_i} (0≤qi,j≤n−10 \le q_{i,j} \le n-1), the desired positions of those birds.

The sum of all pip_i does not exceed 2⋅1052 \cdot 10^5.

Output

Print one line containing one integer, the number of dangerous pairs.

Examples1

  1. Example 1

    Input
    3
    2 1 2
    1 0
    1 1
    
    Expected output
    3