#P1111. 修复公路

修复公路

Description

Given the number of villages NN and the number of roads MM in region A. The roads are bidirectional. You are told which two villages each road connects and when this road will be completed. Find the earliest time when any two villages can travel between each other, i.e., the earliest time when for any pair of villages there exists at least one completed path (possibly consisting of multiple roads).

Input Format

The first line contains two positive integers N,MN, M.

Then the next MM lines follow. Each line contains three positive integers x,y,tx, y, t, indicating that this road connects villages xx and yy, and it will be completed at time tt.

Output Format

If, after all roads are completed, there still exist two villages that cannot travel between each other, output 1-1. Otherwise, output the earliest time when any two villages can travel between each other.

4 4
1 2 6
1 3 4
1 4 5
4 2 3
5

Hint

1x,yN1031\leq x, y\leq N \le 10 ^ 3, 1M,t1051\leq M, t \le 10 ^ 5.

Translated by ChatGPT 5