QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#15512. Bridge Building

统计

Rut, who hasn’t learned to swim, wants to cross a river. Fortunately, a bridge is being constructed over a section of the river, which can be represented as an $N \times M$ grid. Every hour, a bridge segment is built on a tile that was previously covered by water. For example, five bridge segments will have been built after five hours. Rut has obtained a list of the positions of the first bridge segments to be built, and she knows that she can cross the river when she can walk on the bridge from one side of the river to the other without having to swim any part of the way. She starts at row $0$ and wants to reach row $N-1$. These two rows each have a landmass that she can use to walk along the entire river. She can move between tiles that are not covered by water by moving up, down, right, or left. She now wants to know if the given bridge segments are enough to cross, and if so, how many hours she needs to wait before the bridge is sufficiently completed for her to be able to reach the other side.

problem_15512_1.png problem_15512_2.png

Figure 1: Illustration of sample 1 after 4 and 7 hours. Only after 7 hours will there be a path to the other side.

Input

The first line of input contains three integers $N, M$ ($3 \le N, M \le 10^9$), and $T$ ($1 \le T \le 10^5$), the number of rows and columns in the grid, and the number of bridge segments to be built, respectively.

This is followed by $T$ lines, where the $i$-th line contains two integers $R$ ($1 \le R \le N-2$) and $C$ ($0 \le C \le M-1$), the row and column where a tile is completed in the $i$-th hour. Note that in this problem, row $0$ is the bottom row.

Output

If Rut can cross the river, print an integer: the minimum number of hours Rut needs to wait for the bridge to be sufficiently completed to walk to the other side. Otherwise, print “nej” (no).

Example

Input

7 3 8
2 1
1 0
4 1
5 1
2 0
3 0
4 0
3 1

Output

7

Scoring

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

Group Points Constraints
$1$ $10$ $N \le 4$
$2$ $20$ $N, M \le 50$
$3$ $30$ $N, M \le 1000$
$4$ $15$ $T \le 2000$
$5$ $25$ No additional constraints.
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.