Treasure Hunter

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

문제

Chulsu has a treasure map that marks the locations of kk hidden treasures, where each location is presented as a position in the cell of an m×nm \times n grid. Since all the locations in the map except the entrance and exit are hard to access for humans, Chulsu wants to use treasure hunt robot (THR)s to access and collect the treasures. THR always starts from an entrance cell (1,1)(1, 1) and exits through an exit cell (m,n)(m, n) (here, (i,j)(i, j) denotes a cell at the ii-th row and the jj-th column). Also, THR can only go immediately right or below from the current cell and cannot be reused after arriving at the exit cell. The following figure shows an example when m=3m = 3, n=5n = 5, and k =4k = 4 (in the figure, XX denotes a cell where the treasure exists).

(1, 1)    
  XX  
XXXX XX(3, 5)

In the above example, if THR accesses the cells (1,1)(1, 1), (2,1)(2, 1), (3,1)(3, 1), (3,2)(3, 2), (3,3)(3, 3), (3,4)(3, 4), and (3,5)(3, 5) in order, it collects three treasures (at (3,1)(3, 1), (3,2)(3, 2), and (3,4)(3, 4)) after arriving at the exit cell (3,5)(3, 5). In this case, to collect the treasure at the position (2,3)(2, 3), Chulsu needs to use an additional THR. When mm, nn, kk, and kk distinct positions of treasures are given, write a program to compute the minimum number of THR’s to collect all the treasures on the map.

입력

Your program is to read from standard input. The input starts with a line containing three integers, mm, nn, and kk (1m,n,k100,0001 \le m, n, k \le 100,000), where mm is the number of rows, nn is the number of columns of the map, and kk is the number of treasures on the map. In the following kk lines, kk distinct positions of treasures are given as two integers where the two integers represent the row and column positions on the map, respectively.

출력

Your program is to write to standard output. Print exactly one line. The line should contain the minimum number of THR’s to collect all the treasures on the map.