import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int v = sc.nextInt(); int sx = sc.nextInt() - 1; int sy = sc.nextInt() - 1; int gx = sc.nextInt() - 1; int gy = sc.nextInt() - 1; int[][] costs = new int[n][n]; int[][] counts = new int[n][n]; ArrayList>> values = new ArrayList<>(); for (int i = 0; i < n; i++) { Arrays.fill(counts[i], Integer.MAX_VALUE); for (int j = 0; j < n; j++) { costs[i][j] = sc.nextInt(); } } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(sx, sy, 0, v)); while (queue.size() > 0) { Path p = queue.poll(); p.value -= costs[p.y][p.x]; if (p.value <= 0) { continue; } if (counts[p.y][p.x] <= p.count) { continue; } counts[p.y][p.x] = p.count; if (p.x > 0) { queue.add(new Path(p.x - 1, p.y, p.count + 1, p.value)); } if (p.x < n - 1) { queue.add(new Path(p.x + 1, p.y, p.count + 1, p.value)); } if (p.y > 0) { queue.add(new Path(p.x, p.y - 1, p.count + 1, p.value)); } if (p.y < n - 1) { queue.add(new Path(p.x, p.y + 1, p.count + 1, p.value)); } } if (counts[gy][gx] == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(counts[gy][gx]); } } static class Path implements Comparable { int x; int y; int count; int value; public Path(int x, int y, int count, int value) { this.x = x; this.y = y; this.count = count; this.value = value; } public int compareTo(Path another) { if (value == another.value) { return count - another.count; } else { return another.value - value; } } } }