Domino
Togrul has created a simple game for you to test your skills. The objective is to determine the length of the longest chain that can be formed using a given set of dominoes.
Each domino is represented by a pair of numbers a
and b
, indicating the number of dots on each half of the domino. A valid chain is a sequence of dominoes arranged in a line such that for any two consecutive dominoes with indices i
and i+1
, the condition b[i] = a[i+1]
is satisfied.
To find the solution, Togrul asks you to write a program that calculates the length of the longest possible chain.
Input
The first line contains an integer n
(1
<= n
<= 100,000
), representing the number of dominoes.The next n
lines each contain two integers a[i]
and b[i]
(0
<= a[i]
<= b[i]
<= 10^9
), describing the dominoes.
Output
Output a single integer on one line, which is the maximum length of the domino chain.