#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from collections import deque N, V, Sy, Sx, Gy, Gx = map(int, readline().split()) L = tuple(map(int, read().split())) Sx -= 1 Sy -= 1 Gx -= 1 Gy -= 1 S = Sx * N + Sy G = Gx * N + Gy INF = 1000 M = N * N dist = [INF] * (M * 10001) start = M * V + S dist[start] = 0 q = deque([start]) answer = INF while q: x = q.popleft() d = dist[x] V, xy = divmod(x, M) x, y = divmod(xy, N) d += 1 for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): x1 = x + dx y1 = y + dy if not ((0 <= x1 < N) and (0 <= y1 < N)): continue xy1 = x1 * N + y1 V1 = V - L[xy1] if V1 <= 0: continue if xy1 == G: print(d) exit() v = M * V1 + xy1 if dist[v] == INF: dist[v] = d q.append(v) print(-1)