import queue from collections import namedtuple dx = (1, 0, -1, 0) dy = (0, -1, 0, 1) Point = namedtuple('Point', 'x y') def solve(): n, v, sx, sy, gx, gy = [int(i) for i in input().split(' ')] start = Point(x = sx-1, y = sy-1) goal = Point(x = gx-1, y = gy-1) costs = [[int(i) for i in input().split(' ')] for _ in range(n)] mincosts = [[v for _ in range(n)] for _ in range(n)] mincosts[start.y][start.x] = 0 q = queue.Queue() q.put(start) cnt = 0 while True: tmp = queue.Queue() if q.empty() or mincosts[goal.y][goal.x] < v: break while not q.empty(): x, y = q.get() for tx, ty in zip(dx, dy): nx = x + tx ny = y + ty if nx >= 0 and nx < n and ny >= 0 and ny < n: tmpcost = mincosts[y][x] + costs[ny][nx] if mincosts[ny][nx] > tmpcost: mincosts[ny][nx] = tmpcost tmp.put(Point(nx, ny)) q = tmp cnt += 1 if mincosts[goal.y][goal.x] < v: print(cnt) else: print(-1) if __name__ == "__main__": solve()