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]; ArrayList>> values = new ArrayList<>(); for (int i = 0; i < n; i++) { values.add(new ArrayList<>()); for (int j = 0; j < n; j++) { costs[i][j] = sc.nextInt(); values.get(i).add(new HashMap<>()); } } 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 (values.get(p.y).get(p.x).containsKey(p.value)) { continue; } values.get(p.y).get(p.x).put(p.value, 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)); } } int min = Integer.MAX_VALUE; for (int x : values.get(gy).get(gx).values()) { min = Math.min(min, x); } if (min == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(min); } } 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; } } } }