PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000063. Chiếc thang của Takahashi (Ladder Takahashi)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chiếc thang của Takahashi (Ladder Takahashi)

Ladder Takahashi

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A building has floors numbered by positive integers starting at floor 11; floor numbers can be very large. Takahashi starts on floor 11 and wishes to reach the highest possible floor.

There are nn ladders. Ladder ii directly connects floors AiA_i and BiB_i, and may be used in either direction. Takahashi may use a ladder multiple times and change ladders whenever they share a floor. Apart from the listed ladders, there is no way to move between different floors.

Determine the largest floor number reachable from floor 11. If Takahashi cannot leave his starting floor, the answer is 11.

Input

The first line contains nn; each of the next nn lines contains Ai,BiA_i,B_i.

Output

Print the highest reachable floor.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤Ai,Bi≤1091\le A_i,B_i\le10^9, Ai≠BiA_i\ne B_i.
  • Subtask 2 (30 points): 1≤n≤42001\le n\le4200; 1≤Ai,Bi≤1091\le A_i,B_i\le10^9, Ai≠BiA_i\ne B_i.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; 1≤Ai,Bi≤1091\le A_i,B_i\le10^9, Ai≠BiA_i\ne B_i.

Examples

Input

4
1 4
4 3
4 10
8 3

Output

10

Explanation

From floor 1, the ladders lead to floor 4 and then floor 10. No reachable floor exceeds 10, so the answer is 10.