This page is still under construction.

Parts of this page are still being built. What you see may change.

Jakarta Skyscrapers

Time limit1sMemory limit256 MB

Summary
Doge 0 spreads news by jumping between buildings with its own stride or handing it to a doge on the same building, and you count the fewest jumps to doge 1.
Level

Hard8 of 10

Topics
Shortest path, Graph, BFS
Solved
No attempts yet

Problem

Jakarta has NN tall buildings standing in a straight line. They are numbered 0,1,…,N−10, 1, \dots, N-1 from the left. The city has no other tall buildings.

MM mysterious creatures called doges live in the city. The doges are numbered 0,1,…,M−10, 1, \dots, M-1. Doge ii starts on building BiB_i, and the strength of its mysterious power is the positive integer PiP_i. A doge with power pp standing on building bb can move to building b+pb+p or building b−pb-p in one jump. The destination number must be at least 00 and less than NN.

Doge 0 leads all the doges. It has urgent news for doge 1 and wants the news delivered as fast as possible. A doge that has heard the news can do one of two things.

  • Jump to another building with its own power.
  • Pass the news to another doge standing on the same building.

Write a program that computes the smallest number of jumps needed to bring the news to doge 1. If there is no way to deliver it, detect that as well.

Input

The first line contains the integers NN and MM. Each of the next MM lines contains two integers BiB_i and PiP_i, one doge per line.

  • 1≤N≤300001 \le N \le 30000
  • 2≤M≤300002 \le M \le 30000
  • 0≤Bi<N0 \le B_i < N
  • 1≤Pi≤300001 \le P_i \le 30000

Output

Print the smallest number of jumps on the first line. Print −1-1 if the news cannot reach doge 1.

Hint

Consider a city with 5 buildings and 3 doges. Doge 0 is on building 0 with power 2, doge 1 is on building 1 with power 1, and doge 2 is on building 4 with power 1. The following order delivers the news in 5 jumps.

  • Doge 0 jumps to building 2, then jumps to building 4. (2 jumps)
  • Doge 0 passes the news to doge 2 on building 4.
  • Doge 2 jumps to building 3, then building 2, then building 1. (3 jumps)
  • Doge 2 passes the news to doge 1 on building 1.

Examples1

  1. Example 1

    Input
    5 3
    0 2
    1 1
    4 1
    
    Expected output
    5