import java.io.IOException; import java.util.InputMismatchException; import java.util.PriorityQueue; public class Main { int n, v; int sx, sy; int gx, gy; int[][] g; int max; class D implements Comparable { int x, y; int rem; int min; D(int x, int y, int rem, int min) { this.x = x; this.y = y; this.rem = rem; this.min = min; } @Override public int compareTo(D o) { if (this.min != o.min) { return this.min - o.min; } if (this.rem != o.rem) { return this.rem - o.rem; } if (this.x != o.x) { return this.x - o.x; } if (this.y != o.y) { return this.y - o.y; } return 0; } @Override public String toString() { return "(" + x + ", " + y + ", " + rem + ", " + min + ")"; } } int bfs() { PriorityQueue queue = new PriorityQueue(); queue.add(new D(sx, sy, v, 0)); int[] dx = { 0, -1, 1, 0 }; int[] dy = { -1, 0, 0, 1 }; boolean[][][] vis = new boolean[n][n][v + 1]; while (!queue.isEmpty()) { D d = queue.poll(); int x = d.x; int y = d.y; int rem = d.rem; int min = d.min; if (x == gx && y == gy) { return min; } if (vis[y][x][rem]) { continue; } vis[y][x][rem] = true; int mdist = Math.abs(gx - x) + Math.abs(gy - y); int bound = max * mdist; if (bound < rem) { queue.add(new D(gx, gy, 0, min + mdist)); } else { for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || n <= nx || ny < 0 || n <= ny) { continue; } int nrem = rem - g[ny][nx]; if (nrem <= 0) { continue; } if (vis[ny][nx][nrem]) { continue; } int nmin = min + 1; queue.add(new D(nx, ny, nrem, nmin)); } } } return -1; } void run() { MyScanner sc = new MyScanner(); n = sc.nextInt(); v = sc.nextInt(); sx = sc.nextInt() - 1; sy = sc.nextInt() - 1; gx = sc.nextInt() - 1; gy = sc.nextInt() - 1; g = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { g[i][j] = sc.nextInt(); max = Math.max(max, g[i][j]); } } System.out.println(bfs()); } public static void main(String[] args) { new Main().run(); } public void mapDebug(int[][] a) { System.out.println("--------map display---------"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%3d ", a[i][j]); } System.out.println(); } System.out.println("----------------------------" + '\n'); } class MyScanner { int read() { try { return System.in.read(); } catch (IOException e) { throw new InputMismatchException(); } } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }