import collections N,V,Sx,Sy,Gx,Gy = map(int,input().split()) Sx -= 1 Sy -= 1 Gx -= 1 Gy -= 1 sand = [list(map(int,input().split())) for i in range(N)] dxy = [(1,0),(-1,0),(0,-1),(0,1)] lstimes = [[0]*(N) for i in range(N)] lscost = [[float('INF')]*N for i in range(N)] lscost[Sy][Sx] = 0 d = collections.deque([(Sy,Sx)]) while d: x,y = d.popleft() if x == Gy and y == Gx: if lscost[x][y] < V: print(lstimes[x][y]) exit() for dx,dy in dxy: if 0<= x+dx < N and 0 <= y+dy < N: if lscost[x+dx][y+dy] > lscost[x][y] + sand[x+dx][y+dy]: lscost[x+dx][y+dy] = lscost[x][y] + sand[x+dx][y+dy] lstimes[x+dx][y+dy] = lstimes[x][y]+1 d.append((x+dx,y+dy)) print(-1)