#include using namespace std; #define int long long typedef long long ll; typedef pairpint; typedef vectorvint; typedef vectorvpint; #define pb push_back #define mp make_pair #define fi first #define se second #define ln <void chmin(T &t,U f){if(t>f)t=f;} templatevoid chmax(T &t,U f){if(t>N>>V>>sx>>sy>>gx>>gy;sx--;sy--;gx--;gy--; chmin(V,2000); rep(i,N)rep(j,N)cin>>L[i][j]; queueque; que.push(data(sy,sx,0)); fill_n(**dist,100*100*2000,1<<25); dist[sy][sx][0]=0; while(que.size()){ data d=que.front();que.pop(); rep(i,4){ int ny=d.y+dxy[i],nx=d.x+dxy[i+1]; if(ny<0||ny>=N||nx<0||nx>=N)continue; int nh=d.h+L[ny][nx]; if(nh>=V||dist[ny][nx][nh]<=dist[d.y][d.x][d.h]+1)continue; dist[ny][nx][nh]=dist[d.y][d.x][d.h]+1; que.push(data(ny,nx,nh)); } } int mi=1<<25; rep(i,V)chmin(mi,dist[gy][gx][i]); if(mi==(1<<25))cout<<-1<