#include using namespace std; #define REP(x,y)for(ll x=0;x=0;--x) #define per(x,y,z)for(ll x=ll(z)-1;x>=ll(y);--x) #define all(v)begin(v),end(v) #define rall(v)rbegin(v),rend(v) #define pb emplace_back #define fi first #define se second #define lb(v,k)ll(lower_bound(all(v),k)-begin(v)) #define ub(v,k)ll(upper_bound(all(v),k)-begin(v)) #define uniq(v)sort(all(v)),v.erase(unique(all(v)),v.end()) #define sz(x)ll(x.size()) #define out(x)cout<<(x)<<'\n' #define sor(v)sort(all(v)) using ll=long long; using P=pair; using PP=tuple; using PPP=tuple; using vi=vector; using vvi=vector; using vb=vector; using vvb=vector; using vp=vector

; using vvp=vector; struct $_${$_$(){ios::sync_with_stdio(false);cin.tie(nullptr);}}$_$_$; templateinline bool chmin(T&A,T B){if(A>B){A=B;return true;}return false;} templateinline bool chmax(T&A,T B){if(A>N>>V>>sy>>sx>>gy>>gx; --sx; --sy; --gx; --gy; vvi L(N,vi(N)); REP(i,N)REP(j,N)cin>>L[i][j]; vvi dp(N,vi(N,-inf)); dp[sx][sy]=V; REP(i,N*N) { if(dp[gx][gy]>0) { out(i); return 0; } vvi ep(N,vi(N,-inf)); REP(x,N)REP(y,N)REP(j,4) { int nx=x+dx[j]; int ny=y+dy[j]; if(nx<0||ny<0||nx>=N||ny>=N)continue; chmax(ep[nx][ny],dp[x][y]-L[nx][ny]); } dp=move(ep); } out(-1); }