from collections import deque

n, v, sy, sx, gy, gx = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(n)]
sx -= 1
sy -= 1
gx -= 1
gy -= 1
dist = [-1] * (n * n * v)
def f(i, j, k):
    return (i * n + j) * v + k
dist[f(sx, sy, v - 1)] = 0
queue = deque()
queue.append(f(sx, sy, v - 1))
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while queue:
    tmp = queue.popleft()
    x = tmp // (n * v)
    tmp -= x * n * v
    y = tmp // v
    vv = tmp - y * v
    
    for dx, dy in directions:
        nx = x + dx
        ny = y + dy
        if nx == -1 or nx == n or ny == -1 or ny == n:
            continue
        nv = vv - L[nx][ny]
        if nv < 0:
            continue
        if dist[f(nx, ny, nv)] == -1:
            if nx == gx and ny == gy:
                print(dist[f(x, y, vv)] + 1)
                exit()
            dist[f(nx, ny, nv)] = dist[f(x, y, vv)] + 1
            queue.append(f(nx, ny, nv))
print(-1)