#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; const int DY[] = {-1,0,1,0}; const int DX[] = {0,1,0,-1}; int N, V, SX, SY, GX, GY, L[100][100]; bool vis[100][100][10001]; int main(){ cin >> N >> V >> SX >> SY >> GX >> GY; --SX; --SY; --GX; --GY; rep(i, N)rep(j, N)scanf("%d", &L[i][j]); queue> q; q.push(mt(0, SY, SX, V)); while(sz(q)){ int d, y, x, v; tie(d, y, x, v) = q.front(); q.pop(); if(y == GY&&x == GX){ cout << d << endl; return 0; } if(vis[y][x][v])continue; vis[y][x][v] = 1; rep(i, 4){ int ny = DY[i] + y, nx = DX[i] + x; if(ny >= 0 && ny < N&&nx >= 0 && nx < N){ int nv = v - L[ny][nx]; if(nv > 0 && !vis[ny][nx][nv])q.push(mt(d + 1, ny, nx, nv)); } } } puts("-1"); }