Springboards

N과 P개의 축에 나란한 스프링보드가 주어지고, 각 보드는 (x1,y1)에서 (x2,y2)로 이동시키며 x1<=x2, y1<=y2를 만족한다. 오른쪽이나 위로만 움직여 (0,0)에서 (N,N)까지 갈 때 최소 도보 거리를 구한다.

어려움8동적 계획법정렬그래프최단 경로아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

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 (x_1,y_1)(x\_1,y\_1) and if Bessie uses it, she will land at a point (x_2,y_2)(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?

입력

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

The next PP lines each contains four integers, x_1x\_1, y_1y\_1, x_2x\_2, y_2y\_2, where x_1x_2x\_1 \le x\_2 and y_1y_2.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).

힌트

Bessie's best path is:

  • Bessie walks from (0,0) to (0,1) (1 unit).
  • Bessie springs to (0,2).
  • Bessie walks from (0,2) to (1,2) (1 unit).
  • Bessie springs to (2,3).
  • Bessie walks from (2,3) to (3,3) (1 unit).

The total walking length of Bessie's path is 3 units.