import random import sys def is_prime(n): if n == 2: return True if n == 1 or n & 1 == 0: return False d = (n - 1) >> 1 while d & 1 == 0: d >>= 1 for k in range(10): a = random.randint(1, n - 1) t = d y = pow(a, t, n) while t != n - 1 and y != 1 and y != n - 1: y = (y * y) % n t <<= 1 if y != n - 1 and t & 1 == 0: return False return True n=int(input()) used=[False for i in range(2002)] isprime=[False for i in range(2002)] for i in range(3, 2002, 2): isprime[i]=True isprime[2]=True for i in range(3, 2002): if isprime[i]: for j in range(2*i, 2002, i): isprime[j]=False def dfs(x, w): used[x]=True if x%w!=1 and (not used[x-1]) and (not isprime[x-1]): dfs(x-1, w) if x%w!=0 and x+1<=n and (not used[x+1]) and (not isprime[x+1]): dfs(x+1, w) if x>w and (not used[x-w]) and (not isprime[x-w]): dfs(x-w, w) if x+w<=n and (not used[x+w]) and (not isprime[x+w]): dfs(x+w, w) def solve(w): for i in range(1, n+1): used[i]=False dfs(1, w) return used[n] if n<=2000: for i in range(3, n): if solve(i): print(i) sys.exit() else: if n%8!=1: print(8) elif not is_prime(n-8): print(8) elif n%14!=1: print(14) elif not is_prime(n-14): print(14) else: print(17)