Tetris#3149

Tetris

1 second
64 megabytes
Very hard

Anton recently discovered a game called Tetris++, which is similar to the classic Tetris but with some unique twists.

Rules of Tetris++:

The game is played on a grid with a height of and a width of squares. At the start of the game, a piece begins to fall from the top left corner. This piece is a vertical rectangle measuring 3 \times 1, with each square painted a specific color.

When the piece appears on the board, the player can immediately move it any number of cells to the right and can also cyclically shift the colors of the piece upwards any number of times. The piece then falls until it lands on another square or reaches the bottom of the grid.

Once the piece has landed, any sequence of at least squares of the same color, aligned vertically, horizontally, or diagonally, will disappear instantly. All colored squares above the vanished ones will fall down simultaneously. If new sequences of or more squares of the same color are formed after this, they will also disappear, and this process continues.

The objective is to minimize the number of colored squares left on the board.

Help Anton determine how many positions to move the falling piece to the right and how many times to cyclically shift it upwards so that as few colored squares as possible remain on the board.

Input

The first line contains the numbers and , separated by a space (4 \leq N \leq 26, 1 \leq M \leq 15).

Each of the next lines contains numbers, separated by spaces, representing the initial state of the playing field (from top to bottom). Numbers from to denote the color of the corresponding square, and the number indicates no colored square is present at that location.

The last three lines contain three numbers, representing the colors (from to ) of the squares (from top to bottom) of the falling piece.

It is guaranteed that the top four rows of the playing field do not contain any colored squares.

Output

You need to output numbers, separated by spaces. The first number is the number of units to move the piece to the right (from to M - 1). The second number is the number of cyclic shifts required (from to ). The third is the total number of squares that disappeared as a result of the piece's fall. If there are multiple optimal solutions, choose the one with the fewest rightward movements of the piece, and if there are still multiple solutions, choose the one with the fewest movements and shifts.

Examples

Example 1

Loading
Loading