import std.stdio, std.string, std.conv ,std.array,std.algorithm, std.range ,std.math; void main(){ auto buf = readln().strip().split().map!(to!int)(); immutable N = buf[0]; immutable V = buf[1]; immutable Sx = buf[2]; immutable Sy = buf[3]; immutable Gx = buf[4]; immutable Gy = buf[5]; int[][] L = new int[][](N); int[][] dp = new int[][](N); int[][] dp2 = new int[][](N); foreach(immutable int i; 0 .. N) { dp[i] = new int[](N); dp2[i] = new int[](N); L[i] = readln().strip().split().map!(to!int)().array; } dp[Sy-1][Sx-1] = V; //writeln(); //foreach(immutable int i; 0 .. N) { //foreach(immutable int j; 0 .. N) //write(dp[i][j], " "); //writeln; //} bool ch = true; int co = 0; while(ch) { ++co; ch = false; swap(dp, dp2); foreach(immutable int i; 0 .. N) { foreach(immutable int j; 0 .. N) { dp[i][j] = dp2[i][j]; if(i != 0 && dp2[i-1][j]>0 ) { immutable tn = dp2[i-1][j] - L[i][j]; if(dp[i][j] < tn) { dp[i][j] = tn; ch = true; } } if(j != 0 && dp2[i][j-1]>0 ) { immutable tn = dp2[i][j-1] - L[i][j]; if(dp[i][j] < tn) { dp[i][j] = tn; ch = true; } } if(i != N-1 && dp2[i+1][j]>0 ) { immutable tn = dp2[i+1][j] - L[i][j]; if(dp[i][j] < tn) { dp[i][j] = tn; ch = true; } } if(j != N-1 && dp2[i][j+1]>0 ) { immutable tn = dp2[i][j+1] - L[i][j]; if(dp[i][j] < tn) { dp[i][j] = tn; ch = true; } } } } //writeln(); //foreach(immutable int i; 0 .. N) { //foreach(immutable int j; 0 .. N) //write(dp[i][j], " "); //writeln; //} if(dp[Gy-1][Gx-1] > 0) { writeln(co); return; } } writeln(-1); return; }