#include using namespace std; typedef pair P; typedef tuple T; const int INF = 1e7; const int dh[] = {1, -1, 0, 0}; const int dw[] = {0, 0, 1, -1}; int L[100][100]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, V, sx, sy, gx, gy; cin >> N >> V >> sx >> sy >> gx >> gy; sx--; sy--; gx--; gy--; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> L[i][j]; } } vector< vector > memo(N, vector(N, INF)); queue q; q.push(T(0, 0, sy, sx)); int ans = INF; while (!q.empty()) { T t = q.front(); q.pop(); int d, v, h, w; tie(d, v, h, w) = t; if (memo[h][w] <= v) continue; memo[h][w] = v; if (h == gy && w == gx) { ans = d; break; } for (int i = 0; i < 4; i++) { int nh = h + dh[i], nw = w + dw[i]; if (nh < 0 || nh >= N || nw < 0 || nw >= N) continue; int nd = d + 1, nv = v + L[nh][nw]; if (nv >= V) continue; if (memo[nh][nw] <= nv) continue; q.push(T(nd, nv, nh, nw)); } } cout << (ans == INF ? -1 : ans) << endl; return 0; }