#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,V,sx,sy,gx,gy; cin >> N >> V >> sx >> sy >> gx >> gy; swap(sx,sy); swap(gx,gy); V = min(V,2000); sx--; sy--; gx--; gy--; vector> L(N,vector(N)); for(auto &h : L) for(auto &w : h) cin >> w; vector dist(N,vector(N,vector(V+1,-1))); queue> Q; Q.push({sx,sy,V}); dist.at(sx).at(sy).at(V) = 0; vector> dxy = {{-1,0},{0,1},{1,0},{0,-1}}; vector Dxy(N,vector>>(N)); for(int i=0; i= N) continue; Dxy.at(i).at(k).push_back({ii,kk}); } } while(Q.size()){ auto [x,y,v] = Q.front(); Q.pop(); int d = dist.at(x).at(y).at(v); for(auto [nx,ny] : Dxy.at(x).at(y)){ int nv = v-L.at(nx).at(ny); if(nv <= 0) continue; if(dist.at(nx).at(ny).at(nv) != -1) continue; dist.at(nx).at(ny).at(nv) = d+1; Q.push({nx,ny,nv}); } } int answer = 1001001001; for(int i=1; i<=V; i++) if(dist.at(gx).at(gy).at(i) != -1) answer = min(answer,dist.at(gx).at(gy).at(i)); if(answer > 1e9) answer = -1; cout << answer << endl; }