Under construction

Still being built. Things may change

Making a Constellation

Time limit1sMemory limit512 MB

Summary
Attach N tree decorations in order, each red root to the shallowest unconnected blue star, and find the minimum possible maximum depth.
Level

Hard8 of 10

Topics
Tree, Greedy, BFS
Solved
No attempts yet

Problem

A rainbow-colored star is shining in the night sky. Star wants to use this rainbow star and NN decorations to build a constellation and decorate the night sky beautifully.

The decorations have the following properties.

  • Each decoration is a tree made of AiA_i stars and (Ai−1)(A_i-1) strings. A string connects two different stars.
  • Each decoration has exactly one red star. When the red star is treated as the root, every leaf star is blue. All other stars are white.
  • Each star has a number from 1 to AiA_i. The red star is always numbered 1, and every star has a different number.

In a constellation, choose a star and move from the rainbow star to the chosen star along strings. The minimum number of strings passed on this path is the depth of the chosen star.

The length of a constellation is the maximum depth among all stars in it.

Star attaches the decorations one at a time, in the order received. Every decoration must be attached in that order and follow these rules.

  • The red star of the first decoration is connected to the rainbow star with a string.
  • For the ii-th decoration (i>1i > 1), connect its red star with a string to a blue star that has the minimum depth among the blue stars in the constellation built from the first i−1i-1 decorations and is not yet connected to another decoration. If several such stars exist, choose any one.

Star wants to build a constellation that follows these rules and has the minimum possible length. Find the minimum length of a constellation that Star can build.

Input

The first line contains the number of decorations NN. (1≤N≤1000001 \le N \le 100000)

From the second line, the information about the NN decorations is given in the order Star receives them.

For each decoration, the first line gives the number of stars AiA_i. The following Ai−1A_i-1 lines each give two star numbers vv and ww connected by a string, separated by a space. (2≤Ai≤1000002 \le A_i \le 100000; 1≤v,w≤Ai1 \le v, w \le A_i; v≠wv \ne w)

The total number of stars in all decorations does not exceed 500000.

Output

Print the minimum length of a constellation that Star can build.

Examples1

  1. Example 1

    Input
    3
    4
    1 2
    1 3
    3 4
    6
    1 2
    1 3
    2 4
    3 5
    3 6
    6
    1 2
    1 3
    1 4
    2 5
    4 6
    
    Expected output
    6