import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n, v, sx, sy, gx, gy = map(int, input().split()) sx, sy = sx-1, sy-1 gx, gy = gx-1, gy-1 L = [] for i in range(n): l = list(map(int, input().split())) L += l s = sy*n+sx g = gy*n+gx from collections import deque q = [(0, v, s)] q = deque(q) HP = [0]*(n*n) HP[s] = v while q: time, hp, i = q.popleft() y, x = divmod(i, n) if i == g: print(time) exit() for dy, dx in (-1, 0), (1, 0), (0, 1), (0, -1): ny, nx = y+dy, x+dx if 0 <= ny < n and 0 <= nx < n: j = ny*n+nx if hp-L[j] <= 0: continue if hp-L[j] > HP[j]: HP[j] = hp-L[j] q.append((time+1, hp-L[j], j)) print(-1)