#!/usr/bin/env python3 import collections DELTAS = [(1, 0), (-1, 0), (0, 1), (0, -1)] IMPOSSIBLE = -1 def solve(size, start_life, start, goal, stage): life = [[0 for _ in range(size)] for _ in range(size)] life[start[0]][start[1]] = start_life q = collections.deque() q.append((0, start_life, start)) while q: dist, life0, (r0, c0) = q.popleft() if (r0, c0) == goal: return dist for dr, dc in DELTAS: r, c = r0 + dr, c0 + dc if r < 0 or c < 0 or r >= size or c >= size: continue new_life = life0 - stage[r][c] if new_life <= life[r][c]: continue life[r][c] = new_life q.append((dist + 1, new_life, (r, c))) return IMPOSSIBLE def main(): size, start_life, start_c, start_r, goal_c, goal_r = map(int, input().split()) start = (start_r - 1, start_c - 1) goal = (goal_r - 1, goal_c - 1) stage = [[int(l) for l in input().split()] for _ in range(size)] print(solve(size, start_life, start, goal, stage)) if __name__ == '__main__': main()