This page is still under construction.

Parts of this page are still being built. What you see may change.

Byton Tree

Time limit2sMemory limit512 MB

Summary
Given a tree in recursive notation, where each leaf has a time interval, find the minimum number of cuts (each cut picks every byton in the subtree at one moment) so that all intervals are covered.
Level

Medium7 of 10

Topics
Tree, DFS, Greedy, Sorting
Solved
No attempts yet

Problem

Bytons are rare fruits that grow on a byton tree. They appear only on leaf branches, that is, branches from which no other branches grow.

Every byton on one leaf branch shares the same ripeness window, an interval of time during which it may be picked. Picked too early a byton is unripe, picked too late it is rotten. The owner of a byton tree wants to gather every byton so that each one is ripe at the exact moment it is picked, while performing as few cuts as possible.

A cut is made on a branch, right at its base. Cutting a branch immediately picks every byton that grows on it, either directly or through its sub-branches. Any number of cuts may be performed within a single unit of time, and the trunk itself counts as a branch.

Given the tree, find the minimum number of cuts so that all bytons are collected and every collected byton is ripe at the instant it is picked.

Input

The input is a single line that describes the byton tree recursively, starting from the trunk. A branch is described by an integer kk, the number of branches growing on it. If k=0k = 0 the branch is a leaf, and kk is followed by two integers aia_i and bib_i (1≤ai≤bi≤1091 \le a_i \le b_i \le 10^9), the first and last moments of the ripeness window of the bytons on that branch. If k>0k > 0, the descriptions of its kk sub-branches follow. The total number of branches does not exceed 10610^6.

Output

Print a single integer, the minimum number of cuts needed so that every byton is collected and is ripe at the moment it is picked.

Notes

In the sample, bytons grow on three leaf branches, and the intervals in the figure are their ripeness windows. Two cuts are enough to collect everything: for example, cut one branch at time 55 and another at time 88.

Examples1

  1. Example 1

    Input
    2 1 0 3 5 2 0 8 10 1 0 6 9
    
    Expected output
    2