import sequtils,strutils,deques,future type zahyou = tuple[x,y : int] item = tuple[id : zahyou,h : int,cnt : int] var N,V,Sx,Sy,Gx,Gy : int D = [[1,0],[0,1],[-1,0],[0,-1]] (N,V,Sx,Sy,Gx,Gy) = stdin.readline.split.map(parseInt) var sabaku = newSeqWith(N + 2,newSeqWith(N + 2,100000)) S = newSeqWith(N + 2,newSeqWith(N + 2,V)) dq = initDeque[item](1024) p : zahyou t : item t2 : item flag : bool for n in 1..N: sabaku[n][1..N] = stdin.readline.split.map(parseInt) S[Sy][Sx] = 0 p = (Sx,Sy) t = (p,0,0) dq.addLast(t) while dq.len > 0 and flag == false: t = dq.popFirst p = t.id if S[p.y][p.x] < t.h: continue for d in D: var ny = p.y + d[0] nx = p.x + d[1] if S[ny][nx] > t.h + sabaku[ny][nx]: if (nx,ny) == (Gx,Gy): echo t.cnt + 1 flag = true break S[ny][nx] = t.h + sabaku[ny][nx] t2 = ((nx,ny),t.h + sabaku[ny][nx],t.cnt + 1) dq.addLast(t2) if flag == false: echo -1