import sys import math def is_prime(n): if n < 2: return False for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: return False return True def is_valid_path(W, N): def get_position(x): return (x // W, x % W) start = 1 end = N if start == end: return True visited = set() queue = [(get_position(start - 1), [start])] while queue: (x, y), path = queue.pop(0) if (x, y) == get_position(end - 1): return True current = path[-1] if current == end: return True neighbors = [] if x > 0: neighbors.append((x - 1, y)) if x < (N - 1) // W: neighbors.append((x + 1, y)) if y > 0: neighbors.append((x, y - 1)) if y < W - 1: neighbors.append((x, y + 1)) for nx, ny in neighbors: num = nx * W + ny + 1 if num > N: continue if not is_prime(num) and num not in visited: visited.add(num) new_path = path + [num] queue.append(((nx, ny), new_path)) return False def find_min_w(N): if N == 4: return 3 for W in range(1, N): if (W >= N - 1) or is_valid_path(W, N): return W return N - 1 if __name__ == "__main__": N = int(sys.stdin.readline()) print(find_min_w(N))