Skip to main content
LIVE CONTEST: ALGOR Contest: Round #4 (Special Challenge) is live now!

~/algor $ cat problems/graf-yonalishni-ozgartirish

Reversing streets

Medium 1000 ms 128 MB 56%
Share

A city has $n$ intersections and $m$ one-way streets: along a street $u \to v$ one may drive only from $u$ to $v$. The direction of any street may be reversed. What is the smallest number of streets to reverse so that it becomes possible to drive from intersection $1$ to intersection $n$? If no changes help, print $-1$.

One-way streets: a path from the first intersection to the last, with the reversed streets highlighted

Input

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

Output

One integer — the smallest number of reversals 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$

Examples

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

Sign in to submit a solution

Signing up is free and takes a minute.