from heapq import heappush, heappop 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)] INF = 10 ** 18 dist = [[[INF] * 2000 for _ in range(n)] for _ in range(n)] dist[sx][sy][0] = 0 q = [(0, sx, sy, 0)] while q: d, x, y, c = heappop(q) if d > dist[x][y][c]: continue for dx, dy in (1, 0), (0, 1), (-1, 0), (0, -1): nx, ny = x + dx, y + dy if not (0 <= nx < n and 0 <= ny < n): continue nc = c + l[nx][ny] if nc < 2000 and d + 1 < dist[nx][ny][nc]: dist[nx][ny][nc] = d + 1 heappush(q, (d + 1, nx, ny, nc)) ans = min(dist[gx][gy][:v]) if ans == INF: ans = -1 print(ans)