from queue import Queue def solve(): n, v, sy, sx, gy, gx = list(map(int, input().split())) if v > (abs(sx - gx) + abs(sy - gy)) * 9: return abs(sx - gx) + abs(sy - gy) gx, gy = gx - 1, gy - 1 g = [list(map(int, input().split())) for i in range(n)] dis = [[[-1] * (v + 1) for j in range(n)] for i in range(n)] dis[sx - 1][sy - 1][v] = 0; queue = Queue() queue.put((sx - 1, sy - 1, v)) dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] while not queue.empty(): u = queue.get() for i in range(4): x, y = u[0] + dx[i], u[1] + dy[i] if x < 0 or x >= n or y < 0 or y >= n: continue z = u[2] - g[x][y] if z > 0 and dis[x][y][z] == -1: dis[x][y][z] = dis[u[0]][u[1]][u[2]] + 1 if x == gx and y == gy: return dis[x][y][z] queue.put((x, y, z)) return -1 print(solve())