Under construction

Still being built. Things may change

Karosai

Interview

Time limit1sMemory limit1024 MB

Summary
Given ponds joined by partitions with fixed heights, find the minimum water level at pond 1 that allows a path to pond N.
Level

Medium4 of 10

Topics
Minimum spanning tree, Union-find, Graph
Solved
No attempts yet

Problem

Karosas Rosas sails on a pond system made of NN ponds. Some of the ponds are connected, so you can cross from one to another. Each connection has a partition with a fixed height, written hi,jh_{i,j}, where hi,j=hj,ih_{i,j} = h_{j,i}. Karosas can cross from pond ii to pond jj only when the water level in pond ii is at least hi,jh_{i,j}. For example, there are three ponds (N=3N = 3). Ponds 1 and 2 are joined by a partition with h1,2=5000h_{1,2} = 5000, and ponds 2 and 3 are joined by a partition with h2,3=7000h_{2,3} = 7000. If the water level in pond 1 is at least 5000, Karosas can cross from pond 1 to pond 2. To reach pond 3 from pond 1, the level must be at least 7000. In the picture, Karosas can get from pond 1 to pond 2 but not to pond 3. Karosas Rosas is staying in pond 1, and his friend is in pond NN. Rosas wants to know how high the water level in pond 1 must be so that he can visit his friend. Given the pond setup, find the minimum level that pond 1 must be raised to so that pond NN can be reached from pond 1.

Input

The first line contains two integers NN and MM, the number of ponds and the number of connected pairs. Each of the next MM lines contains three integers ii, jj, and hi,jh_{i,j}, meaning ponds ii and jj are joined by a partition of height hi,jh_{i,j}. (1≤i<j≤N1 \le i < j \le N, and hi,j=hj,ih_{i,j} = h_{j,i}.)

Output

Print one integer, the minimum water level in pond 1 needed to reach pond NN from pond 1. The input guarantees that a path from pond 1 to pond NN always exists.

Constraints

1<N≤1 0001 < N \le 1\,000, 0<M≤N(N−1)20 < M \le \frac{N(N-1)}{2}, 0<hi,j≤1090 < h_{i,j} \le 10^9

Examples2

  1. Example 1

    Input
    3 2
    1 2 5000
    2 3 7000
    
    Expected output
    7000
    
  2. Example 2

    Input
    4 5
    1 2 5000
    1 3 2000
    1 4 10000
    2 4 4000
    3 4 3000
    
    Expected output
    3000