QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#18588. Shortest Time

Estadísticas

There is a number line of length $N-1$. The goal is to start at $1$ on the number line and reach $N$ in the shortest possible time. There are controls occurring every minute for a total of $T$ minutes.

The control at the $t$-th minute is given by two integers $l_t$ and $r_t$. If your position $x$ satisfies $l_t \le x \le r_t$ at time $t+0.5$, you fail immediately. If $t > T$, no controls occur.

Every minute, you can move to an adjacent cell or stay in your current cell. You succeed as soon as you reach $N$.

If there is a way to reach cell $N$ without failing, output the shortest time required; otherwise, output -1.

Input

The first line contains the length of the number line $N$. $(1 \le N \le 2 \times 10^5)$

The second line contains the total time $T$ during which controls occur. $(1 \le T \le 2 \times 10^5)$

From the third line, $T$ lines follow, each containing two integers $l_t$ and $r_t$ separated by a space. $(1 \le l_t \le r_t \le N)$

Output

Output the shortest time. If it does not exist, output -1.

Examples

Input 1

4
4
2 4
1 1
2 2
3 3

Output 1

4

Input 2

5
3
2 5
3 4
1 4

Output 2

-1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.