#!/usr/bin/env python3 import sys from collections import deque MOD = 998244353 input = sys.stdin.readline powerMax = 99 * 2 * 9 INF = 10 ** 16 def main(): N, V, Sx, Sy, Gx, Gy = map(int, input().split()) power_init = min(V, powerMax) G = [list(map(int, input().split())) for _ in range(N)] startY = Sy - 1 startX = Sx - 1 goalY = Gy - 1 goalX = Gx - 1 q = deque() dist = [[[INF] * (powerMax + 1) for _ in range(N)] for _ in range(N)] q.append((0, startY, startX, power_init)) dist[startY][startX][power_init] = 0 while q: min_cost, nowY, nowX, now_power = q.popleft() if nowY == goalY and nowX == goalX: print(min_cost) return for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]: nexty = nowY + dy nextx = nowX + dx if nexty < 0 or nextx < 0 or nexty >= N or nextx >= N: continue next_power = now_power - G[nexty][nextx] if next_power <= 0 or dist[nexty][nextx][next_power] != INF: continue q.append((min_cost + 1, nexty, nextx, next_power)) dist[nexty][nextx][next_power] = min_cost + 1 print(-1) return if __name__ == '__main__': main()