Knight Story

Time limit1sMemory limit128 MB

Summary
Assign N knights to N distinct target cells on an infinite chessboard to minimize the total number of knight moves.
Level

Medium7 of 10

Topics
Dynamic programming, Shortest path, Graph, Bit manipulation
Solved
No attempts yet

Problem

There is an infinitely large chessboard. NN knights are placed on distinct cells of this board. In addition, NN cells of the board are specially marked; these are called target cells. Every target cell differs from the starting cell of every knight.

Write a program that computes the minimum total number of moves needed to move all NN knights onto the target cells. The knights are identical and indistinguishable, so you may freely decide which knight goes to which target cell. Several knights may occupy the same cell at the same time, but in the end each target cell must contain exactly one knight.

A knight moves in an L-shape: two cells in one direction and one cell perpendicular to it. That is, from a cell (x,y)(x, y) it can move to one of the eight cells (x±1,y±2)(x \pm 1, y \pm 2) or (x±2,y±1)(x \pm 2, y \pm 1).

Input

The input consists of several test cases. The first line of each test case contains the number of knights (which equals the number of target cells) NN. (1≤N≤151 \le N \le 15) Each of the next NN lines contains two integers xx and yy, the starting position of a knight. Each of the following NN lines contains the coordinates xx and yy of a target cell. All coordinates fit in a signed 32-bit integer.

The last line of the input contains a single 00, which marks the end of the input.

Output

For each test case, print one line in the following format.

k. m

Here kk is the test case number (starting from 1) and mm is the minimum total number of moves needed to move all knights onto the target cells.

Examples5

  1. Example 1

    Input
    2
    3 5
    6 5
    5 3
    7 3
    0
    
    Expected output
    1. 3
    
  2. Example 2

    Input
    1
    0 0
    1 2
    0
    
    Expected output
    1. 1
    
  3. Example 3

    Input
    1
    0 0
    1 0
    0
    
    Expected output
    1. 3
    
  4. Example 4

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

    Input
    1
    0 0
    1 2
    1
    5 5
    5 8
    0
    
    Expected output
    1. 1
    2. 3