Under construction

Still being built. Things may change

Your life

Interview

Time limit1sMemory limit256 MB

Summary
Find the fewest directed moves from node 1 to node N in a graph where every edge goes forward, or report -1 when the goal is unreachable.
Level

Easy3 of 10

Topics
BFS, Shortest path, Graph
Solved
No attempts yet

Problem

The situations you can run into while chasing a dream are drawn as a graph. All of them are compressed into NN situations numbered 1 to NN. You are in situation 1 right now, and situation NN is the single dream you want to reach.

A situation changes according to your choice. There are MM changes you can choose from, each given as a pair xx, yy. It means that while you are in situation xx you may choose to move to situation yy, and x<yx < y is guaranteed.

Can you reach the dream? If you can, how many changes does it take when you keep the number of changes as small as possible?

Input

The first line contains the natural numbers NN and MM separated by a space, where 1≤N≤1000001 \le N \le 100000 and 0≤M≤2000000 \le M \le 200000.

Each of the next MM lines contains the natural numbers xx and yy separated by a space, where 1≤x<y≤N1 \le x < y \le N. The same change may be given more than once.

Output

If you can reach the dream, print the minimum number of situation changes it takes. Otherwise print -1.

Hint

In the first example you move from situation 1 to situation 2 and then from situation 2 to situation 4, so two changes are enough.

Examples2

  1. Example 1

    Input
    4 4
    1 2
    2 3
    3 4
    2 4
    
    Expected output
    2
    
  2. Example 2

    Input
    1 0
    
    Expected output
    0