from collections import defaultdict import heapq def solve(): N, V, Sx, Sy, Gx, Gy = list(map(int, input().split())) L = [] for i in range(N): L.append(list(map(int, input().split()))) Sy -= 1 Sx -= 1 Gy -= 1 Gx -= 1 dy = (-1, 0, 1, 0) dx = (0, -1, 0, 1) dist = defaultdict(lambda: float('inf')) hq = [] heapq.heappush(hq, (0, Sy, Sx, V)) while hq: n, y, x, v = heapq.heappop(hq) if y == Gy and x == Gx: print(n) return for d in range(4): ny = y + dy[d] nx = x + dx[d] if 0 <= nx < N and 0 <= ny < N and L[ny][nx] < v: nv = v - L[ny][nx] if dist[ny, nx, nv] > n + 1: dist[ny, nx, nv] = n + 1 heapq.heappush(hq, (n + 1, ny, nx, nv)) print(-1) if __name__ == '__main__': solve()