Under construction

Still being built. Things may change

Honey Heist

Interview

Time limit2sMemory limit512 MB

Summary
Build the adjacency of a hexagonal honeycomb of side R, remove wax cells, and find the shortest number of chewed cells from A to B, checking it against N.
Level

Medium6 of 10

Topics
Graph, BFS, Implementation, Geometry
Solved
No attempts yet

Problem

The scout ant 0x67 is looking for food and finds a beehive nearby. Inside the comb there is a cell packed with dried honey, and carrying that honey back to the nest would feed the colony through the winter. To reach the honey, 0x67 has to chew a tunnel through the cells of the comb.

The comb is a regular hexagon whose every edge is RR cells long, so it holds R3−(R−1)3R^3 - (R-1)^3 cells. The cells are numbered in row major order: rows from top to bottom, and within a row from left to right. Two cells are adjacent when they share a wall. The numbering for R=4R = 4 is:

Cell numbering for R = 4

0x67 goes through the hole and lands in cell AA. It does not chew that cell. From there it chews one adjacent cell at a time on its way to cell BB, which holds the honey. Its jaw gives out after a while, so it can chew at most NN cells. Some cells are hardened with wax and cannot be chewed, so 0x67 has to work around them.

Before it starts, 0x67 computes KK, the smallest number of cells it has to chew to reach cell BB, so that BB is the KKth cell it chews. If K>NK > N, it is not strong enough to dig the tunnel.

Input

The first line contains five integers RR, NN, AA, BB, XX separated by spaces.

  • RR: the number of cells along each edge of the comb (2≤R≤202 \le R \le 20). The comb holds R3−(R−1)3R^3 - (R-1)^3 cells.
  • NN: the largest number of cells 0x67 can chew (1≤N<R3−(R−1)31 \le N < R^3 - (R-1)^3).
  • AA: the ID of the cell 0x67 enters. This cell lies on the border of the comb, so it has fewer than six neighbors.
  • BB: the ID of the cell that holds the honey (1≤B≤R3−(R−1)31 \le B \le R^3 - (R-1)^3).
  • XX: the number of wax hardened cells (0≤X<(R3−(R−1)3)−10 \le X < (R^3 - (R-1)^3) - 1).

The second line contains the XX IDs of the wax hardened cells, separated by spaces. When X=0X = 0 the second line is empty.

AA, BB and the IDs on the second line are all different, and each of them is an integer between 11 and R3−(R−1)3R^3 - (R-1)^3.

Output

If 0x67 can reach the honey, print KK, the smallest number of cells it has to chew. If the wax seals off cell BB or K>NK > N, print No instead.

Hint

The first example, K = 6

This is the comb of the first example. The black cells are hardened with wax, and the six green cells are the ones 0x67 chews.

The second example, No

The second example uses the same comb with N=3N = 3. After three red cells the jaw gives out, so the honey stays out of reach.

Examples4

  1. Example 1

    Input
    6 6 1 45 11
    15 16 17 19 26 27 52 53 58 65 74
    
    Expected output
    6
    
  2. Example 2

    Input
    6 3 1 45 11
    15 16 17 19 26 27 52 53 58 65 74
    
    Expected output
    No
    
  3. Example 3

    Input
    2 2 1 7 0
    
    
    Expected output
    2
    
  4. Example 4

    Input
    6 1 1 8 0
    
    
    Expected output
    1