All problems

Problem 941

Minimum Score of a Path Between Two Cities

Medium · 20 pts · Graph, Union Find, Depth-First Search

Problem

Cities 1 to N are joined by bidirectional roads with distances. The score of a path is the smallest distance of any road on it, and a path may revisit cities and roads as often as it likes. Find the minimum possible score of a path from city 1 to city N; a path between them is guaranteed to exist. Because roads can be reused, any road in the connected component of city 1 can be included, so the answer is the smallest road distance in that component.

Input

The first line contains N and M. Each of the next M lines contains a road u v d.

Output

Print the minimum possible score.

Constraints

2 <= N <= 100000; 1 <= M <= 100000; 1 <= d <= 10^4; cities 1 and N are connected

Example input

4 4
1 2 9
2 3 6
2 4 5
1 4 7

Example output

5

Explanation

With roads 1-2 (9), 2-3 (6), 2-4 (5) and 1-4 (7), the path 1 → 2 → 4 has roads 9 and 5, giving a score of 5. No road shorter than 5 is reachable, so the answer is 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number of Good Paths Next: Largest Color Value in a Directed Graph