Under construction

Still being built. Things may change

Horseback Riding

Time limit1sMemory limit512 MB

Summary
Move k knights on an 8x8 board into the lowest rows, leftmost cells first, in at most 1500 knight moves without two knights ever sharing a cell.
Level

Medium7 of 10

Topics
Simulation, BFS, Greedy
Solved
No attempts yet

Statement

It is well known that chess is a sport. Dima, who is into horseback riding, considers it boring and not a sport at all. His friend Sasha decided to show him how complex and interesting chess can be.

Sasha placed kk knights on an 8×88 \times 8 chessboard. The knights are on a promenade and want to return to their stables, but they do not remember the way home.

The knights are in their stables if the following conditions hold. The lowest k div 8k \text{ div } 8 rows are filled with knights, and the next row contains knights only in its leftmost cells. If k mod 8≠0k \bmod 8 \ne 0, then k mod 8k \bmod 8 knights take the leftmost positions in that row.

The target cells for k=11k=11 knights, one per knight.

Knights move as they do in chess: exactly two cells in one direction, then exactly one cell in another direction.

Possible moves of a knight

Knights move one at a time, in any order. At any moment, at most one knight can occupy a cell. Dima spent two days on this problem. Find a sequence of knight moves such that no two knights ever occupy the same cell and all knights end up in their stables. You do not have to minimize the number of moves, but the total must not exceed 1500.

Input

The first line contains an integer kk, the number of knights on the board (1≤k≤641 \leq k \leq 64). Each of the following kk lines contains one knight position in the form xy, where x is a column letter and y is a row digit. Columns run from a (left) to h (right), and rows run from 1 (bottom) to 8 (top).

Output

On the first line, print the number of moves. Then print the moves one per line, in the order they are made, in the form xy-zt, where x and z are column letters and y and t are row digits. You do not have to minimize the number of moves, but the total must not exceed 1500.

Examples2

  1. Example 1

    Input
    2
    a5
    f1
    
    Expected output
    4
    a5-b3
    b3-a1
    f1-d2
    d2-b1
    
  2. Example 2

    Input
    3
    a1
    b3
    c2
    
    Expected output
    5
    b3-d2
    d2-b1
    c2-b4
    b4-d3
    d3-c1