from random import randrange from queue import Queue def is_prime( n ): if n <= 1 or ( n > 2 and ~n & 1 ): return False if n == 2 or n == 3: return True for i in range( 100 ): a = randrange( 2, n - 1, 1 ) p = n - 1 if pow( a, p, n ) != 1: return False while ~p & 1: x = pow( a, p, n ) if x != 1 and x != n - 1: return False p >>= 1 return True N = int( input() ) if N < 100: for w in range( 1, N + 1, 1 ): dd = [ -1, 1, -w, w ] vis = [ False for i in range( N + 1 ) ] que = Queue() vis[ 1 ] = True que.put( 1 ) while not que.empty(): u = que.get() if u == N: exit( print( w ) ) for di in range( 4 ): nu = u + dd[ di ] if not ( 1 <= nu and nu <= N ): continue if is_prime( nu ): continue if vis[ nu ]: continue vis[ nu ] = 1 que.put( nu ) else: if N % 8 != 1 or not is_prime( N - 8 ): print( 8 ) else: print( 14 )