Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/graf-eng-arzon-yol

Cheapest path

Medium 2000 ms 128 MB 55%
Share

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$.

A graph of cities with prices on the edges; the cheapest path from the first city to the last is highlighted

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

Sign in to submit a solution

Signing up is free and takes a minute.