#P1640. Consecutive Equipment Attack

    ID: 14926 Type: Default 1000ms 256MiB

Consecutive Equipment Attack

Consecutive Equipment Attack

lxhgww is obsessed with a game where he owns many equipments. Each equipment has two attributes with values in the range [1,10000][1, 10000]. When using an equipment, only one of its attributes can be chosen, and each equipment can be used at most once. To deal damage to the ultimate boss, the sequence of attribute values used in consecutive attacks must start at 11 and increase by one each time, i.e., 1,2,3,1, 2, 3, \dots. Given the attributes of all equipments, determine the maximum number of consecutive attacks that lxhgww can perform. This problem can be solved by modeling it as a bipartite matching problem: match each required attack value (starting from 11) to a unique equipment that contains that value as one of its attributes.

inputFormat

The first line contains an integer nn, the number of equipments. Each of the following nn lines contains two integers aa and bb (1a,b100001 \le a, b \le 10000), representing the two attributes of an equipment.

outputFormat

Output a single integer, which is the maximum number of consecutive attacks (starting from 11) that can be performed.

sample

3
1 2
2 3
1 1
3