import sys from heapq import * sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] dij = [(0, 1), (1, 0), (0, -1), (-1, 0)] def main(): n,v,sj,si,gj,gi=MI() sj, si, gj, gi=sj-1,si-1,gj-1,gi-1 ll=LLI(n) maxhp=[[0]*n for _ in range(n)] bfs=[] #[距離,体力,行,列] heappush(bfs,[0,v,si,sj]) while bfs: dist,hp,i,j=heappop(bfs) maxhp[i][j]=hp for di,dj in dij: ni,nj=i+di,j+dj if ni<0 or nj<0 or ni>=n or nj>=n:continue nhp=hp-ll[ni][nj] if nhp<=maxhp[ni][nj]:continue if (ni, nj) == (gi, gj): print(dist+1) exit() heappush(bfs,[dist+1,nhp,ni,nj]) print(-1) main()