#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; #define FOR(i, s, e) for (ll(i) = (s); (i) < (e); (i)++) #define FORR(i, s, e) for (ll(i) = (s); (i) > (e); (i)--) #define debug(x) cout << #x << ": " << x << endl #define mp make_pair #define pb push_back const ll MOD = 1000000007; const int INF = 1e8; const ll LINF = 1e16; const double PI = acos(-1.0); int dx[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int dy[8] = { 1, -1, 0, 0, 1, -1, 1, -1 }; /* ----- xtimex Problem: yukicoder034 / Link: http://yukicoder.me/problems/no/34 ----- */ /* ------問題------ -----問題ここまで----- */ /* -----解説等----- ----解説ここまで---- */ ll N, V; int sx, sy, tx, ty; int masu[100][100]; int dist[100][100][10010]; ll ans = 0LL; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N >> V; cin >> sx >> sy >> tx >> ty; sx--; sy--; tx--; ty--; FOR(i, 0, N) { FOR(j, 0, N)cin >> masu[i][j]; } //input FOR(i, 0, 100)FOR(j, 0, 100)FOR(k,0,10010)dist[i][j][k] = INF; using tp = tuple; queueq; q.push(tp(sy, sx, V)); dist[sy][sx][V] = 0; ans = -1; while (!q.empty()) { //debug(q.size()); int y, x, v; tie(y, x, v) = q.front(); q.pop(); if (y == ty&&x == tx) { cout << dist[ty][tx][v] << endl; return 0; } FOR(i, 0, 4) { int ny = y + dy[i]; int nx = x + dx[i]; if (0 <= ny&&ny < N && 0 <= nx&&nx < N) { int nv = v - masu[ny][nx]; if (nv > 0 && dist[ny][nx][nv] == INF) { dist[ny][nx][nv] = dist[y][x][v] + 1; q.push(tp(ny, nx, nv)); } } } } cout << ans << endl; return 0; }