#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int u, v; ll w; }; ll MOD = 1000000007; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; int main() { int N, V, SX, SY, GX, GY; cin >> N >> V >> SX >> SY >> GX >> GY; SX--; SY--; GX--; GY--; vector< vector > L(N, vector(N)), a(N, vector(N)); for (int y = 0; y < N; y++) for (int x = 0; x < N; x++) cin >> L[y][x]; a[SY][SX] = V; for (int i = 0; i < N * N; i++) { for (int y = 0; y < N; y++) for (int x = 0; x < N; x++) { if ((i + x + y + SX + SY) % 2) continue; for (int j = 0; j < 4; j++) { int _x = x + dx[j], _y = y + dy[j]; if (_x >= 0 && _y >= 0 && _x < N && _y < N) a[_y][_x] = max(a[_y][_x], a[y][x] - L[_y][_x]); } } if (a[GY][GX] > 0) { cout << i + 1 << endl; return 0; } } cout << -1 << endl; }