import sys input = sys.stdin.readline from collections import deque def bfs(): dist = [[[-1]*(V+1) for _ in range(N)] for _ in range(N)] dist[Sx][Sy][V] = 0 q = deque([(Sx, Sy, V)]) while q: x, y, v = q.popleft() for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]: if 0<=nx0 and dist[nx][ny][v-L[nx][ny]]==-1: dist[nx][ny][v-L[nx][ny]] = dist[x][y][v]+1 q.append((nx, ny, v-L[nx][ny])) return dist N, V, Sy, Sx, Gy, Gx = map(int, input().split()) Sy -= 1 Sx -= 1 Gy -= 1 Gx -= 1 L = [list(map(int, input().split())) for _ in range(N)] if V>=1800: print(abs(Sx-Gx)+abs(Sy-Gy)) exit() dist = bfs() ans = 10**18 for i in range(V+1): if dist[Gx][Gy][i]!=-1: ans = min(ans, dist[Gx][Gy][i]) if ans==10**18: print(-1) else: print(ans)