Moving Cells

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Little Alice has got a modern pixel picture for her birthday.

The picture is a rectangular grid of size n×mn\times m. Each column of the grid has one or more consecutive cells colored black, all the other cells are colored white.

Alice considers the picture beautiful if there is a path between any two black cells uu and vv that runs only through the black cells, each time going from a cell to a side-adjacent cell --- begin in the black cell uu, then go to a side adjacent to uu black cell ww, then go to a side adjacent to ww black cell, and so on, eventually reaching the black cell vv.

Since the picture is modern, it can be changed. In one action you may select any column and move all black cells in that column one cell in the same direction --- up or down. Cells can be moved only if they do not go outside the picture.

Alice wonders what is the minimum number of actions it would take to get a beautiful black picture.

입력

The first line of input has two integers nn and mm --- the number of rows and the number of columns in the picture, respectively (1n,m100,0001 \le n, m \le 100\\,000). It is guaranteed that the total number of the picture cells does not exceed 10610^{6} (1nm1,000,0001 \le n \cdot m \le 1\\,000\\,000).

The next mm lines contain two integers s_is\_i and t_it\_i each --- the starting and the ending positions of black cells in the ii-th column of the grid (1s_it_in1 \le s\_i \le t\_i \le n).

출력

Output a single integer --- the minimum number of actions you need to make the given picture beautiful.