#463
Unrated
Springboards
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Bessie is in a 2D grid where walking is permitted only in directions parallel to one of the coordinate axes. She starts at the point (0,0)(0,0) and wishes to reach (N,N)(N,N) (1N1091\le N\le 10^9). To help her out, there are PP (1P1051\le P\le 10^5) springboards on the grid. Each springboard is at a fixed point (x1,y1)(x_1,y_1) and if Bessie uses it, she will land at a point (x2,y2)(x_2,y_2).

Bessie is a progress-oriented cow, so she only permits herself to walk up or right, never left nor down. Likewise, each springboard is configured to never go left nor down. What is the minimum distance Bessie needs to walk?

SCORING:

Test cases 2-5 satisfy P1000P \le 1000.Test cases 6-15 satisfy no additional constraints.

입력

The fist line contains two space-separated integers NN and PP.

The next PP lines each contains four integers, x1x_1, y1y_1, x2x_2, y2y_2, where x1x2x_1 \le x_2 and y1y2.y_1 \le y_2.

All springboard and target locations are distinct.

출력

Output a single integer, the minimum distance Bessie needs to walk to reach (N,N)(N,N).

예제 입력 1

3 2
0 1 0 2
1 2 2 3

예제 출력 1

3
코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.