Battlecode


Submit solution

Points: 1
Time limit: 1.0s
Python 3 2.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
C, C++, Java, Python

To help with your government's new space mining project, you have been sent to Titan-14 to mine the rare mineral Axionite. Resources on this moon are extremely limited, so your mining operation needs to be as efficient as possible.

Titan-14 can be represented as an infinite grid of tiles. You have already built a Axionite extractors, each occupying a single tile at some coordinates. You also have a base, which occupies a 3 \times 3 block of tiles at some coordinates.

You can construct straight lines of conveyor belts between any pair of extractors, or between an extractor and the base, to transport Axionite. Each line of conveyor tiles must connect two buildings (either extractors or the base). Sadly, Titan-14 a hostile place, and there are b obstacles scattered across the terrain, and lines of conveyor belts cannot pass through obstacles.

Is it possible to connect all extractors to the base? If so, output the minimum number of conveyor belts required to do so.

Input

The first line contains two integers, a and b (1 \leq a \leq 10^5, 0 \leq b \leq 10^5), the number of Axionite extractors and obstacles on Titan-14.

The next a lines contain two integers, a_x and a_y (1 \leq a_x, a_y \leq 10^9), indicating that there is an extractor at coordinates (a_x, a_y).

The next b lines contain two integers, b_x and b_y (1 \leq b_x, b_y \leq 10^9), indicating that there is an obstacle at coordinates (b_x, b_y).

The last line contains two integers, h_x and h_y (1 \leq h_x, h_y \leq 10^9), indicating that the top-left corner of yor base is at coordinates (h_x, h_y). Unlike extractors and obstacles, your base occupies a 3 \times 3 space.

Output

Output the minimum number of conveyor belts needed to connect all extractors to your base. If this is not possible, output -1.

Clarifications

  • You can only construct lines of conveyor belts between clear horizontals/verticals between two buildings.
  • You do not need to place conveyor belts on extractors, or on your base.

Example

Input 1
5 3
1 1
8 1
1 3
3 8
5 8
7 5
2 6
4 8
3 3
Output 1
12

Shown below is how you could connect the extractors to get the optimal number of conveyor belts, 12.

Image 1

Input 2
1 1
5 1
4 1
1 1
Output 2
-1

It is not possible to connect the only extractor to your base, as there is an obstacle in between.

Image 2

Input 3
4 1
1 7
3 7
5 7
7 7
5 5
3 1
Output 3
6

Image 3

Input 4
4 1
1 7
3 7
5 7
7 7
4 7
3 1
Output 4
8

Image 4


Comments

There are no comments at the moment.