E. Explosive Slabstones Rearrangement
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Barbara has a garden. The garden can be represented as a $$$n \times m$$$ grid. She has placed $$$k$$$ slabstones in the garden, so she can enjoy stepping slabstones from one to another every day. The slabstones are indexed from $$$1$$$ to $$$k$$$. Each slabstone fully occupies some cell of the garden, and no two slabstones are placed at the same cell.

However, an evil person, Babara, is going to place a special device that will occupy a rectangular region in the garden. If any slabstone overlaps with the device, it will explode and destroy the whole garden!

To avoid the explosion, Barbara wants to rearrange the slabstones by shifting the slabstones within the garden. The slabstones should remain non-overlapping during slabstone rearrangement. The energy spent is equal to the largest index among all slabstones that have been moved. Now Barbara wants to know the minimum energy required to rearrange the slabstones, so she can save her energy for "other purposes".

For example, suppose the device will be placed at the blue rectangle. Then Barbara can rearrange the slabstones as follows. Please note that the slabstones do not overlap during the whole process, not only after the rearrangement. All slabstones that have been moved have index at most $$$4$$$, so the energy spent is $$$4$$$.

Input

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$.

Followed by $$$k$$$ lines, the $$$i$$$-th of which contains two integers $$$x_i$$$ and $$$y_i$$$, representing that the $$$i$$$-th slabstone is located at the $$$y_i$$$-th cell of the $$$x_i$$$-th row.

The last line contains four integers $$$u_1$$$, $$$v_1$$$, $$$u_2$$$ and $$$v_2$$$, representing that the top-left corner of the device is located at the $$$v_1$$$-th cell of the $$$u_1$$$-th row, and the bottom-right corner of the device is located at the $$$v_2$$$-th cell of the $$$u_2$$$-th row.

Output

Print the minimum energy required to rearrange the slabstones. If no slabstones need to be moved, the energy spent is $$$0$$$. If the explosion cannot be avoided, just output $$$-1$$$.

  • $$$1 \leq n \leq 500$$$
  • $$$1 \leq m \leq 500$$$
  • $$$1 \leq k \leq n \times m$$$
  • $$$1 \leq x_i \leq n$$$
  • $$$1 \leq y_i \leq m$$$
  • $$$1 \leq u_1 \leq u_2 \leq n$$$
  • $$$1 \leq v_1 \leq v_2 \leq m$$$
  • All $$$(x_i, y_i)$$$ pairs are distinct.
Examples
Input
3 5 8
1 4
1 5
2 3
2 2
3 3
3 4
2 5
1 2
1 3 1 5
Output
4
Input
3 3 1
2 2
1 1 3 3
Output
-1