import sys import io, os input = sys.stdin.buffer.readline #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 import heapq hq = [(0, v, s)] HP = [0]*(n*n) HP[s] = v heapq.heapify(hq) visit = [False]*(n*n) while hq: time, hp, i = heapq.heappop(hq) 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] heapq.heappush(hq, (time+1, hp-L[j], j)) print(-1)