import sys def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def main(): n,v,sj,si,gj,gi=MI() sj, si, gj, gi=sj-1,si-1,gj-1,gi-1 ll=LLI(n) maxhp=[[0]*n for _ in range(n)] now={} now[si,sj]=v cnt=1 while now: #print(now) nxt={} for (i,j),hp in now.items(): for ni,nj in [[i+1,j],[i-1,j],[i,j+1],[i,j-1]]: if ni<0 or nj<0 or ni>=n or nj>=n:continue nhp=hp-ll[ni][nj] if nhp<=maxhp[ni][nj]:continue if (ni,nj)==(gi,gj): print(cnt) exit() maxhp[ni][nj]=nhp nxt[ni,nj]=nhp now=nxt cnt+=1 print(-1) main()