#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int N, V, sx, sy, gx, gy; cin >> N >> V >> sx >> sy >> gx >> gy; --sx, --sy, --gx, --gy; V = min(V, 18*(N-1)); vector> L(N, vector(N)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> L[i][j]; } } vector dist(N, vector>(N, vector(V, 1e9))); using T = tuple; priority_queue, greater> pq; dist[sx][sy][0] = 0; pq.push({0, sx, sy, 0}); while (!pq.empty()) { int d, x, y, t; tie(d, x, y, t) = pq.top(); pq.pop(); if (dist[x][y][t] < d) continue; for (int k = 0; k < 4; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (0 <= nx && nx < N && 0 <= ny && ny < N) { int nt = t + L[ny][nx]; if (nt < V && dist[nx][ny][nt] > d + 1) { dist[nx][ny][nt] = d + 1; pq.push({d + 1, nx, ny, nt}); } } } } int ans = *min_element(dist[gx][gy].begin(), dist[gx][gy].end()); cout << (ans < 1e9 ? ans : -1) << endl; }