from Queue import deque N,V,Sx,Sy,Gx,Gy = map(int,raw_input().split()) Sx -= 1; Sy -= 1; Gx -= 1; Gy -= 1 L = [map(int,raw_input().split()) for i in xrange(N)] dxy = ((1,0),(0,1),(-1,0),(0,-1)) Q = deque([(Sx,Sy,V,0)]) visited = [[0]*N for i in xrange(N)] visited[Sy][Sx] = V while Q: hx,hy,v,c = Q.popleft() if (hx,hy) == (Gx,Gy): print c; break if v < visited[hy][hx]: continue for dx,dy in dxy: nx = hx + dx; ny = hy + dy if 0 <= nx < N and 0 <= ny < N and v - L[ny][nx] > visited[ny][nx]: visited[ny][nx] = v - L[ny][nx] Q.append((nx,ny,v - L[ny][nx],c+1)) else: print -1