import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines n, v, sx, sy, gx, gy=map(int, readline().split()) l=list(map(int, read().split())) sx-=1 sy-=1 gx-=1 gy-=1 INF=10**5 d={} d[sy*n+sx]=0 hmn=[v]*(n*n) hmn[sy*n+sx]=0 from collections import deque que=deque() que.append(sy*n+sx) while que: hxy=que.popleft() h, xy=divmod(hxy, n*n) x, y=divmod(xy, n) for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): x1, y1=x+dx, y+dy if x1<0 or x1>=n or y1<0 or y1>=n: continue xy1=x1*n+y1 h1=l[xy1]+h if h1>=hmn[xy1]: continue hxy1=h1*n*n+xy1 if (hxy1 not in d) or d[hxy1]>d[hxy]+1: hmn[xy1]=h1 d[hxy1]=d[hxy]+1 if x1==gy and y1==gx: print(d[hxy1]) exit() que.append(hxy1) print(-1)