Transformation move game

Find the fewest turns to reach a target cell on an N by N board where you can walk one cell per turn in normal mode or, after paying t turns, warp to the nearest warp cell in a direction.

Medium6GraphBFSShortest pathNo attempts yetTime limit2sMemory limit512 MB

Problem

A character moves on an N×NN \times N board from the start cell to the target cell. The character starts on (1,1)(1, 1) in normal mode. The target cell is (r,c)(r, c).

The character is either in normal mode or in transformation mode. In normal mode, one turn moves the character by one cell up, down, left, or right. In transformation mode, one turn moves the character to the nearest warp cell in one of the four directions from the current cell. When the chosen direction holds no warp cell, the character cannot move that way. In transformation mode only warp moves are allowed.

Changing from normal mode to transformation mode takes tt turns, and changing back takes no turns. Compute the minimum number of turns needed to reach the target cell from the start cell.

Input

The first line holds the board size NN (1N500)(1 \le N \le 500), the transformation cost tt (0t500)(0 \le t \le 500), and the target row rr and column cc (1rN,1cN)(1 \le r \le N, 1 \le c \le N), separated by spaces.

The next NN lines describe warp cells. Each line has NN characters, and each character is # or .. # marks a cell with a warp point and . marks a plain cell.

Output

Print the minimum number of turns needed to reach the target cell in one line. The target stays reachable on foot alone, so the answer always exists.

Hint

Walking in normal mode alone reaches the target. Transformation pays off when warp cells line up, and returning is free, so compare the arrival times of both modes on the same cell. A warp move from a warp cell still targets the nearest warp cell in the chosen direction, not the current cell.