시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB748610.714%

문제

In 21XX, the residents of the IOI planet plan to immigrate to a recently discovered planet.

The new planet has a field, which is a rectangular grid with R rows and C columns. The columns are parallel to the south-north direction, and the rows are parallel to the east-west direction. The cell in the i-th row from north and the j-th column from west is called the cell (i, j). The north-west corner of the field is the cell (1, 1), and the south-east corner is the cell (R,C). In each year, the residents of the IOI planet choose the direction of the wind blowing on the field. The direction is one of east, west, south, or north.

To engage in agriculture in the new planet, they will plant “JOI grasses” in all over the field in the new planet. In the spring of the first year of immigration, N cells in the field have JOI grasses.

The range of JOI grasses are expanded by the wind. Each summer, the seeds of JOI grasses are blown by the wind to the direction chosen by the residents of the IOI planet. The seeds move one cell to the direction of the wind, and they land on. If the seeds land on a cell of the field without JOI grasses, it will have JOI grasses in the spring of the next year. Once a cell has JOI grasses, it will keep JOI grasses in the future.

We want to calculate the minimum number of years until all the cells in the field have JOI grasses if we adjust the direction of the wind appropriately.

Write a program which calculates the minimum number of years until all the cells in the field have JOI grasses if we adjust the direction of the wind appropriately.

입력

Read the following data from the standard input.

  • The first line of input contains two space separated integers R, C. This means the field is a rectangular grid with R rows and C columns.
  • The second line of input contains an integer N, the number of cells in the field which have JOI grasses in the spring of the first year of immigration.
  • The i-th line (1 ≤ i ≤ N) of the following N lines contains two space separated integers Si, Ei. This means the cell (Si, Ei) has JOI grasses in the spring of the first year of immigration.

출력

Write one line to the standard output. The output contains the minimum number of years until all the cells in the field have JOI grasses if we adjust the direction of the wind appropriately.

제한

  • 1 ≤ N ≤ 300.
  • 1 ≤ R ≤ 1 000 000 000.
  • 1 ≤ C ≤ 1 000 000 000.
  • 1 ≤ Si ≤ R (1 ≤ i ≤ N).
  • 1 ≤ Ei ≤ C (1 ≤ i ≤ N).
  • In the spring of the first year of immigration, there is a cell in the field without JOI grasses.
  • (Si, Ei) ≠ (Sj, Ej) (1 ≤ i < j ≤ N).

서브태스크 1 (5점)

  • R ≤ 4.
  • C ≤ 4.

서브태스크 2 (10점)

  • R ≤ 40.
  • C ≤ 40.

서브태스크 3 (15점)

  • R ≤ 40.

서브태스크 4 (30점)

  • N ≤ 25.

서브태스크 5 (20점)

  • N ≤ 100.

서브태스크 6 (20점)

There are no additional constraints.

예제 입력 1

3 4
3
1 2
1 4
2 3

예제 출력 1

3

In Sample Input 1, the following cells have JOI grasses in the spring of the first year of immigration.

The field in the new planet; cells with ‘0’ have JOI grasses in the spring of the first year of immigration.

In this sample input, if we choose the directions of the wind for the first 3 years are west, south, south, then all the cells will have JOI grasses in the spring after 3 years. The numbers in the following table describe when each cell has JOI grasses in the spring. This is the minimum number of years.

예제 입력 2

4 4
4
1 1
1 4
4 1
4 4

예제 출력 2

4

채점 및 기타 정보

  • 예제는 채점하지 않는다.