from collections import deque import sys input = sys.stdin.readline def sieve(n): is_prime = [True]*(n+1) if n >= 0: is_prime[0] = False if n >= 1: is_prime[1] = False for i in range(2,int(n**0.5)+1): if is_prime[i]: for j in range(i*i,n+1,i): is_prime[j] = False return is_prime def find_shift(c0, d0, can_loop, is_prime, LIMIT): """Find minimal x >= 0 s.t. c0+x and d0+x are prime, but if can_loop is False only x=0 is allowed.""" if not can_loop: return 0 if c0<=LIMIT and d0<=LIMIT and is_prime[c0] and is_prime[d0] else None for x in range(0, LIMIT+1): cc = c0 + x dd = d0 + x if cc <= LIMIT and dd <= LIMIT and is_prime[cc] and is_prime[dd]: return x return None def try_variant(H, W, grid, Sx, Sy, Gx, Gy, DIRS, is_prime, LIMIT): # 1) BFS from S to build shortest‐path tree (parent) and visited q = deque() q.append((Sx, Sy)) visited = [[False]*W for _ in range(H)] visited[Sx][Sy] = True parent = {} while q: x,y = q.popleft() for d,(dx,dy) in enumerate(DIRS): nx,ny = x+dx, y+dy if 0<=nxG path = [] cur = (Gx,Gy) while cur != (Sx,Sy): (px,py), d = parent[cur] path.append(d) cur = (px,py) path.reverse() A0 = path.count(0) # down B0 = path.count(1) # up C0 = path.count(2) # right D0 = path.count(3) # left base_len = len(path) # 3) Check if any vertical / horizontal adjacency exists in visited‐component vert_ok = horz_ok = False for x in range(H): for y in range(W): if visited[x][y]: if x+1 0: order3.append((1,0)) # down else: order3.append((-1,0)) # up if ΔH > 0: order3.append((0,1)) # right else: order3.append((0,-1)) # left # then the other two for d in [(1,0),(-1,0),(0,1),(0,-1)]: if d not in order3: order3.append(d) DIRS3 = order3 ans = min( try_variant(H,W,grid,Sx,Sy,Gx,Gy,DIRS1,is_prime,LIMIT), try_variant(H,W,grid,Sx,Sy,Gx,Gy,DIRS2,is_prime,LIMIT), try_variant(H,W,grid,Sx,Sy,Gx,Gy,DIRS3,is_prime,LIMIT), ) print(ans if ans < float('inf') else -1) if __name__ == "__main__": solve()