import sys import heapq def area_chk(a, b): return a >= 0 and a < N and b >=0 and b < N N, V, Sx, Sy, Gx, Gy = map(int, input().split()) Sx -= 1 Sy -= 1 Gx -= 1 Gy -= 1 sand = [list(map(int, input().split())) for _ in range(N)] dy = [1, 0, -1, 0] dx = [0, 1, 0, -1] dp = [[[0] * (V + 1) for _ in range(N)] for _ in range(N)] dp[Sy][Sx][V] = 1 h = [] heapq.heappush(h, [Sy, Sx, V]) while len(h) != 0: y, x, v = heapq.heappop(h) for i in range(4): ny = y + dy[i] nx = x + dx[i] if not area_chk(ny, nx): continue nv = v - sand[ny][nx] if nv <= 0: continue if dp[ny][nx][nv] != 0: continue if ny == Gy and nx == Gx: print(dp[y][x][v]) sys.exit() dp[ny][nx][nv] = dp[y][x][v] + 1 heapq.heappush(h, [ny, nx, nv]) print(-1)