Traffic!

HandTop Co. is developping a a new game, called Traffic! for his line of hand-held computers. Each level of the game is a puzzle where you have to drive a block to the exit of a room filled with other blocks. Of course the other blocks are in the way, and of course, not every movement is possible for each type of block. To make things even more challenging, the user is informed of the minimal number of possible moves so that, even if he solves the puzzle, he can rate its solution against the best possible solution(s). An example of puzzle is on the right: The room is a 6×6 board, such that the upper-left position has coordinates (0,0) and the bottom-right square has position (5, 5). The room may contain 5 different types of blocks:

1. The white block is the block that needs to be exited as indicated by the black arrow. There is only a single white block and it can only make horizontal moves.
2. Vertical blocks of length 2 can only make vertical moves.
3. Vertical blocks of length 3 can only make vertical moves.
4. Horizontal blocks of length 2 can only make horizontal moves.
5. Horizontal blocks of length 3 can only make horizontal moves. A block move (horizontal or vertical) can only be a round number of positions. For instance, the block labeled 1 has the choice between 3 legal moves (1 position to the right, 2 positions to the right, or 3 positions to the right). The white block, labeled 3, has no legal move. Only the white block can move out of the room. Your goal is, given an initial configuration, compute the minimum number of legal moves to exit the white block. Input The first line of the input is N, the number of puzzles for which you have to compute the solution, followed by the N specifications of each puzzle. A puzzle is specified on five successive lines:
6. the coordinates of the white block;
7. the number and coordinates of vertical blocks of length 2;
8. the number and coordinates of vertical blocks of length 3;
9. the number and coordinates of horizontal blocks of length 2;

2/2 5. the number and coordinates of horizontal blocks of length 3. The position of a block is the coordinate of the upper-left square it occupies. The coordinate is a pair of integers, each in the range [0, 5], such that the first number identifies the row, while the second identifies the column. Thus, the upper-left square of the screen has coordinates (0,0), the upper- right square has coordinates (0, 5), the lower-left has coordinates (5, 0), and the lower-right square has coordinates (5, 5). You can assume that there will always be a sequence of moves to exit the white block. Output For each puzzle, the output of your program should produce a line indicating the minimal number of moves to exit the white block. Note: The Sample Input corresponds to the example drawn above. Sample Input 1 21 140 3101305 20044 152 Sample Output The minimal number of moves to solve puzzle 1 is 8.