#include "bits/stdc++.h" using namespace std; #define REP(i, n) for(int i=0; i<(n); i++) int N,V; typedef pair pos; int pos::*x = &pos::first; int pos::*y = &pos::second; pos s, g; int L[200][200]; int HP[200][200]; int C[200][200]; int INF = 1e9; signed main() { cin >> N >> V>>s.*x>>s.*y>>g.*x>>g.*y; s.*x -= 1; s.*y -= 1; g.*x -= 1; g.*y -= 1; REP(y,N)REP(x,N) cin >> L[x][y]; REP(y,N)REP(x,N) { HP[x][y] = INF; C[x][y] = INF; } HP[s.*x][s.*y] = 0; // スタート地点は消費HP0で行ける C[s.*x][s.*y] = 0; // スタート地点は0移動で行ける queue que; que.push(s); int dxy[] = {0, 1, 0, -1, 0}; while(que.size()) { pos p = que.front(); que.pop(); REP(i,4) { int nx = p.*x + dxy[i]; int ny = p.*y + dxy[i+1]; if (nx<0||N<=nx||ny<0||N<=ny) continue; if (L[nx][ny] + HP[p.*x][p.*y] >= V) continue; // 死ぬので行けない if (nx == g.*x && ny == g.*y) { cout << C[p.*x][p.*y] + 1 << endl; return 0; } if (HP[nx][ny] > L[nx][ny] + HP[p.*x][p.*y]) { HP[nx][ny] = L[nx][ny] + HP[p.*x][p.*y]; C[nx][ny] = C[p.*x][p.*y] + 1; que.push(pos(nx, ny)); } } } cout << -1 << endl; return 0; }