Unlocking Blocks

No attempts yetTime limit1sMemory limit128 MB

Problem

A little-known fact about cows is that they love puzzles! There is a mechanical puzzle made of three solid objects, each built from $1 \times 1$ unit squares glued together. Each object is a "connected" shape: from any square of the object you can reach any other square of the object by stepping north, south, east, or west through squares that belong to it.

An object can be moved by repeatedly sliding it one unit north, south, east, or west. The goal is to separate the objects, so that their bounding boxes (the smallest rectangle enclosing each object) no longer share any positive-area overlap with one another. Given the shapes and locations of the three objects, determine the minimum number of individual one-unit slides required to separate them.

Input

  • Line 1: Three space-separated integers $N_1$, $N_2$, and $N_3$, the number of unit squares making up objects 1, 2, and 3 respectively.
  • Next $N_1$ lines: the $(x, y)$ coordinate of the south-west (bottom-left) corner of a square belonging to object 1.
  • Next $N_2$ lines: the south-west corner of each square of object 2.
  • Next $N_3$ lines: the south-west corner of each square of object 3.

All coordinates lie in the range $0$ to $9$, inclusive.

Output

  • Line 1: The minimum number of one-unit slides needed to separate the three objects, or $-1$ if they cannot be separated.

Hint

For example, suppose object 1 is made of $12$ squares, object 2 of $3$ squares, and object 3 of $5$ squares. Sliding object 3 east by one unit, then object 2 north by one unit, then object 1 west by three units leaves the three bounding boxes with no overlap, separating them in $5$ slides total.