~/algor $ cat problems/graf-eng-arzon-yol
Cheapest path
Medium
2000 ms
128 MB
55%
A region has $n$ cities and $m$ two-way roads. The cities are numbered from $1$ to $n$. Each road joins two cities, and travelling along it costs $w$ soums. What is the smallest amount to spend to get from city $1$ to city $n$? If it is impossible, print $-1$.
Input
The first line contains two integers: $n$ and $m$. Each of the next $m$ lines contains three positive integers: $u$, $v$ and $w$ — the cities joined by the road and the cost of travel.
Output
One integer — the smallest cost or $-1$.
Note
$2 \le n \le 10^{5}$
$0 \le m \le 2 * 10^{5}$
$1 \le u, v \le n$, $u \ne v$
$1 \le w \le 10^{9}$
Examples
Input #1
4 5 1 2 1 2 4 7 1 3 4 3 4 2 2 3 2
Output #1
5
Input #2
3 1 1 2 5
Output #2
-1