#include #include using namespace std; int cost[110][110]; int dp[2][110][110]; int INF = 1000000; int dir[] = { 0, 1, 0, -1, 0 }; int main(void){ int N, V, NN, SX, SY, GX, GY; cin >> N >> V >> SY >> SX >> GY >> GX; NN = N*N; for (int i = 0; i < N; i++){ cost[i][0] = cost[0][i] = cost[i][N + 1] = cost[N + 1][i] = INF; } for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ cin >> cost[i + 1][j + 1]; dp[0][i + 1][j + 1]=dp[1][i + 1][j + 1] = INF; } } dp[0][SX][SY] = 0; for (int i = 1; i <= NN; i++){ for (int x = 1; x <= N; x++){ for (int y = 1; y <= N; y++){ for (int j = 0; j < 4; j++){ dp[i & 1][x + dir[j]][y + dir[j + 1]] = min(dp[i & 1][x + dir[j]][y + dir[j + 1]], dp[(i & 1) ^ 1][x][y] + cost[x + dir[j]][y + dir[j + 1]]); } dp[i&1][x][y] = min(dp[i&1][x][y], dp[(i & 1) ^ 1][x][y]); } } if (dp[i&1][GX][GY] < V){ cout << i << endl; return 0; } } cout << -1 << endl; return 0; }