from collections import deque n,hp,sx,sy,gx,gy = map(int, input().split()) e = [[int(i) for i in input().split()] for j in range(n)] cost = [[0xFFFFFFFF for _ in range(n)] for _ in range(n)] direction = ((0,1),(0,-1),(1,0),(-1,0)) def bsf(): q = deque() q.append(((sx - 1) << 16) + ((sy - 1) << 8) + 0) cost[sy - 1][sx - 1] = 0 while(q): v = q.popleft() vx = (v >> 16) & 0xFF vy = (v >> 8) & 0xFF vc = v & 0xFF nowcost = cost[vy][vx] if vx == gx - 1 and vy == gy - 1: return vc for d in direction: dx = vx + d[1] dy = vy + d[0] if dx < 0 or dy < 0 or dy >= n or dx >= n: continue nextcost = nowcost + e[dy][dx] if cost[dy][dx] > nextcost and nextcost < hp: cost[dy][dx] = nextcost q.append((dx << 16) + (dy << 8) + (vc + 1)) return -1 print(bsf())