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

~/algor $ cat problems/eng-qisqa-yol

Shortest path

Medium 2000 ms 128 MB 45%
Share

A city has $n$ intersections and $m$ two-way streets. The intersections are numbered from $1$ to $n$. Each street joins two intersections, and driving along it takes $1$ minute. Find the smallest number of minutes needed to get from intersection $1$ to intersection $n$. If it is impossible, print $-1$.

A graph of intersections and streets, with the shortest path from the first intersection to the last highlighted

Input

The first line contains two integers: $n$ and $m$. Each of the next $m$ lines contains two positive integers: $u$ and $v$ — the intersections joined by a street.

Output

One integer — the smallest number of minutes or $-1$.

Note

$2 \le n \le 10^{5}$
$0 \le m \le 2 * 10^{5}$
$1 \le u, v \le n$

Examples

Input #1
4 4
1 2
2 3
3 4
1 3
Output #1
2
Input #2
3 1
1 2
Output #2
-1

Sign in to submit a solution

Signing up is free and takes a minute.