Train Line Construction

On an N by N grid with resident counts and blocked cells, find a 4-direction path between two stations minimizing the sum of cell weights along it.

Medium5GraphShortest pathHeapDynamic programmingInterviewNo attempts yetTime limit1sMemory limit64 MB

Problem

MRT Corp is building railways across the country and has hired you.

The map is a square grid with NN cells on each side. Each cell holds the number of residents CC who live there. Everyone living on a cell the railway passes through has to move, so you pick the line between station AA and station BB that forces the fewest residents to move, and report that number. Some cells cannot carry track at all because the ground is unstable or a hill is in the way.

Track cannot run diagonally. It only connects cells up, down, left, and right. The line starts on the cell holding station AA and ends on the cell holding station BB, and both station cells are part of the line. The number of residents who move is the sum of CC over every cell the line passes through.

Input

The first line contains NN, the side length of the map. (2N4002 \le N \le 400)

The second line contains four integers AxA_x, AyA_y, BxB_x, ByB_y: the xx and yy coordinates of station AA, then the xx and yy coordinates of station BB. The xx coordinate is counted from the left and the yy coordinate from the top. The two stations sit on different cells. (1Ax,Ay,Bx,ByN1 \le A_x, A_y, B_x, B_y \le N)

Each of the next NN lines contains NN integers CC separated by spaces. The jjth number on the iith of these lines is the value of the cell with xx coordinate jj and yy coordinate ii. A cell that cannot carry track is given as 1-1. (1C106-1 \le C \le 10^6)

Output

Print the smallest number of residents who have to move, on one line.

If the two stations cannot be connected without laying track on a forbidden cell, print 1-1. If either station sits on a forbidden cell, no line can be built, so print 1-1 in that case too.