Patrick's Parabox is a
Sokoban-like game. A Sokoban puzzle is a gird, and each cell is a wall or a floor. There are several boxes and a player in some distinct floor cells, and they can not move to the wall cells or coincide. You can control the player to move in one of four directions, left, right, up, and down. When the player touches a box, it can push the box. The target is to move all boxes to some target cells.
Please read the following rules carefully. They may be different from the usual rules In this problem, there is
only one box, and the box is the grid itself. That means if the player moves out of the grid, it may be ``teleported'' to a cell adjacent to the box; if the player moves to the box, it may be ``teleported'' to a cell on the boundary of the grid. Besides, there is also a target cell for the player. The player needs to move to the target cell at the end too.
Given a puzzle, you need to find the
minimum times to push the box, such that the box and the player move to the target cell separately.
The following are the detailed and formal rules.
Consider an

grid. Denote
)
as the cell in

-th row and

-the column. The rows are numbered

,

,

,

from top to bottom, and the columns are numbered

,

,

,

from left to right.
Denote W, S, A, and D as the control commands, which means to move up, down, left, and right separately.
Define that
)
,
)
,
)
,
)
.
Define that
)
,
)
,
)
,
)
.
In each operation, you can choose one of the control commands

, one of W, S, A, and D. Denote

as the cell which contains the player and

as the cell which contains the box before the operation:
- If
and
is a floor cell, the player moves to
and the box moves to
. Only this case will be counted in the answer. - If
is a wall cell, nothing happens. - If
is a floor cell and
, the player moves to
. - If
and
is outside the grid, nothing happens. - If
,
is a wall cell and
is a wall cell, nothing happens. - If
,
is a wall cell and
is a floor cell, the player moves to
. - if
is outside the grid and
is a wall cell, nothing happens. - if
is outside the grid and
is outside the grid, nothing happens. - if
is outside the grid and
is a floor cell, the player moves to
.
Note that the above are listed for covering all possibilities, but the operations are valid in only four of them.