from collections import deque def bfs(H, W, L, V, sx, sy, gx, gy): INF = 1<<32 dx = [0, 0, -1, 1] dy = [-1, 1, 0, 0] Cost = [[INF] * W for _ in range(H)] Cost[sx][sy] = 0 Queue = deque() Queue.append((sx, sy, 0)) ret = INF while Queue: x,y,cnt = Queue.popleft() for i in range(len(dx)): nx,my = x + dx[i], y + dy[i] if nx < 0 or H <= nx or my < 0 or W <= my: continue if Cost[x][y] + L[nx][my] < Cost[nx][my] and Cost[x][y] + L[nx][my] < V: Cost[nx][my] = Cost[x][y] + L[nx][my] Queue.append((nx, my, cnt+1)) if nx == gx and my == gy: ret = min(ret, cnt+1) if ret == INF: ret = -1 return ret N,V,Sy,Sx,Gy,Gx = map(int,input().split()) Sx -= 1 Sy -= 1 Gx -= 1 Gy -= 1 L = [list(map(int,input().split())) for _ in range(N)] print(bfs(N, N, L, V, Sx, Sy, Gx, Gy))