#include using namespace std; int main() { int n, v, sx, sy, gx, gy; cin >> n >> v >> sx >> sy >> gx >> gy; --sx, --sy, --gx, --gy; vector> l(n, vector(n)); for (auto& i : l) { for (int& j : i) cin >> j; } vector> u(n, vector(n)); u[sy][sx] = v; queue> que; que.push(make_pair(sy, sx)); const int dy[] = {0, -1, 0, 1}; const int dx[] = {1, 0, -1, 0}; for (int i = 1; !que.empty(); ++i) { queue> next; while (!que.empty()) { auto now = que.front(); que.pop(); int y = now.first, x = now.second; for (int k = 0; k < 4; ++k) { int yy = y + dy[k], xx = x + dx[k]; if (yy < 0 || n <= yy || xx < 0 || n <= xx) continue; if (u[yy][xx] >= u[y][x] - l[yy][xx]) continue; if (yy == gy && xx == gx) { cout << i << endl; return 0; } u[yy][xx] = u[y][x] - l[yy][xx]; next.push(make_pair(yy, xx)); } } que = next; } cout << -1 << endl; }