#include typedef long long int ll; using namespace std; int mat[111][111]; int dp[111][111]; ll n,hp,ssx,ssy,ggx,ggy; int main(){ cin>>n>>hp>>ssx>>ssy>>ggx>>ggy; for(int y=1;y<=n;++y){ for(int x=1;x<=n;++x){ scanf("%d",&(mat[y][x])); ++mat[y][x]; }} //fill(&dp[0][0],&dp[110][110]+1,101010); queue q; hp+=mat[ssy][ssx]-1; q.push((ssx<<8)|(ssy)|(hp<<32)); while(!q.empty()){ ll d=q.front();q.pop(); ll x=(d>>8)&0xff; ll y=(d)&0xff; ll s=(d>>16)&0xffff; ll h=(d>>32)&0xffff; if ((h-=mat[y][x]-1)<=0) continue; if (x==ggx && y==ggy){ cout << s << endl; exit(0); } ++s; if (mat[y-1][x] && h>dp[y-1][x]){ dp[y-1][x]=h; q.push((y-1)|((x)<<8)|(s<<16)|(h<<32)); } if (mat[y+1][x] && h>dp[y+1][x]){ dp[y+1][x]=h; q.push((y+1)|((x)<<8)|(s<<16)|(h<<32)); } if (mat[y][x-1] && h>dp[y][x-1]){ dp[y][x-1]=h; q.push((y)|((x-1)<<8)|(s<<16)|(h<<32)); } if (mat[y][x+1] && h>dp[y][x+1]){ dp[y][x+1]=h; q.push((y)|((x+1)<<8)|(s<<16)|(h<<32)); } } cout<<"-1"<