Hexerpents of Hexwamp#4871

Hexerpents of Hexwamp

1 secondo
64 megabyte
Molto difficile

Hexwamp is a strange swamp, paved with regular hexagonal dimples. Hexerpents crawling in this area are serpents adapted to the environment, consisting of a chain of regular hexagonal sections. Each section fits in one dimple.

Hexerpents crawl by moving some of their sections from the dimples they are in to adjacent ones. To avoid breaking their bodies, sections that are adjacent to each other before the move should also be adjacent after the move. When one section moves, sections adjacent to it support the move and thus cannot move at that time. Any number of sections, as long as no two of them are adjacent to each other, can move at the same time.

You can easily find that a hexerpent can move its sections at either end to only up to two dimples, and can move intermediate sections to only one dimple, if any.

For example, without any obstacles, a hexerpent can crawl forward twisting its body as shown in Figure 1, left to right. In this figure, the serpent moves four of its eight sections at a time, and moves its body forward by one dimple unit after four steps of moves. Actually, they are much better at crawling sideways, like sidewinders.

Figure 1: Crawling forward

Their skin is so sticky that if two sections of a serpent that are not originally adjacent come to adjacent dimples (Figure 2), they will stick together and the serpent cannot help but die. Two sections cannot fit in one dimple, of course. This restricts serpents' moves further. Sometimes, they have to make some efforts to get a food piece even when it is in the dimple next to their head.

Figure 2: Fatal case

Hexwamp has rocks here and there. Each rock fits in a dimple. Hexerpents' skin does not stick to rocks, but they cannot crawl over the rocks. Although avoiding dimples with rocks restricts their moves, they know the geography so well that they can plan the fastest paths.

You are appointed to take responsibility as the head of the scientist team to carry out academic research on this swamp and the serpents. You are expected to accomplish the research, but never at the sacrifice of any casualty. Your task now is to estimate how soon a man-eating hexerpent may move its head (the first section) to the position of a scientist in the swamp. Their body sections except for the head are quite harmless, and the scientist, wearing a high-tech anti-sticking suit, can stay in the same dimple with a body section of the hexerpent.

Input

The input is a sequence of several datasets, and the end of the input is indicated by a line containing a single . The number of datasets never exceeds .

Each dataset looks like the following:

— the number of sections the serpent has

x_1\ y_1

x_2\ y_2

x_n\ y_n

— the number of rocks the swamp has

u_1\ v_1

u_2\ v_2

u_k\ v_k

The first line of the dataset has an integer that indicates the number of sections the hexerpent has (2\le n\le 8). Each of the following lines contains two integers and that indicate the coordinates of a serpent's section. The lines show the initial positions of the sections from the serpent's head to its tail, in this order.

The next line of the dataset indicates the number of rocks, , the swamp has (0\le k\le 100). Each of the following lines contains two integers and that indicate the position of a rock.

Finally, a line contains two integers and , indicating the goal position of the hexerpent, where the scientist is. The serpent's head is not initially here.

All of the coordinates , , , , , and are between and , inclusive. Two integers in a line are separated by a single space. No characters other than decimal digits, minus signs, and spaces to separate two integers appear in the input. The coordinate system used to indicate a position is as shown in Figure 3.

Figure 3: The coordinate system

Output

For each dataset, output a line that contains a decimal integer that indicates the minimum number of steps the serpent requires for moving its head to the goal position. Output lines should not contain any other characters.

You can assume that the hexerpent can reach the goal within steps.

Esempi

Esempio 1

Loading
Loading