import java.math.BigInteger; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; public class Main { public static int simulate(final int N){ boolean[] is_prime = new boolean[N + 1]; Arrays.fill(is_prime, 2, N + 1, true); for(int i = 2; i * i <= N; i++){ if(!is_prime[i]){ continue; } for(int j = i * 2; j <= N; j += i){ is_prime[j] = false; } } if(is_prime[N]){ return -1; } boolean[] visited = new boolean[N]; for(int mid = 1; mid <= N; mid++){ Arrays.fill(visited, false); LinkedList queue = new LinkedList(); queue.add(0); visited[0] = true; while(!queue.isEmpty()){ final int pos = queue.poll(); final int number = pos + 1; if(number == N){ break; } //System.out.println(mid + " => " + pos + " : " + (pos % mid)); if(pos % mid > 0 && !is_prime[number - 1] && !visited[pos - 1]){ queue.add(pos - 1); visited[pos - 1] = true; } if(pos % mid < (mid - 1) && !is_prime[number + 1] && !visited[pos + 1]){ queue.add(pos + 1); visited[pos + 1] = true; } if((pos - mid) > 0 && !is_prime[number - mid] && !visited[pos - mid]){ queue.add(pos - mid); visited[pos - mid] = true; } if((pos + mid) < N && !is_prime[number + mid] && !visited[pos + mid]){ queue.add(pos + mid); visited[pos + mid] = true; } } if(visited[N - 1]){ return mid; } } return -1; } public static int smart(final BigInteger N){ final BigInteger four = BigInteger.valueOf(4); final BigInteger six = BigInteger.valueOf(6); final BigInteger eight = BigInteger.valueOf(8); final BigInteger nine = BigInteger.valueOf(9); final BigInteger ten = BigInteger.valueOf(10); final BigInteger twelve = BigInteger.valueOf(12); final BigInteger fourteen = BigInteger.valueOf(14); final BigInteger fifteen = BigInteger.valueOf(15); final BigInteger sixteen = BigInteger.valueOf(16); final BigInteger twenty = BigInteger.valueOf(20); final BigInteger twentyone = BigInteger.valueOf(21); final BigInteger twentytwo = BigInteger.valueOf(22); final BigInteger twentyfour = BigInteger.valueOf(24); final BigInteger twentyfive = BigInteger.valueOf(25); if(N.equals(four)){ return 3; }else if(N.equals(six)){ return 5; }else if(N.equals(eight) || N.equals(nine) || N.equals(ten) || N.equals(fifteen) || N.equals(sixteen) || N.equals(twentytwo)){ return 7; }else if(N.equals(twelve)){ return 11; }else if(N.equals(fourteen)){ return 13; }else if(N.equals(twenty) || N.equals(twentyone)){ return 19; }else if(N.equals(twentyfour) || N.equals(twentyfive)){ return 23; }else if(N.subtract(BigInteger.ONE).mod(eight).equals(BigInteger.ZERO) && N.subtract(eight).isProbablePrime(1000000000)){ return 14; }else{ return 8; } } // シュミレーションして法則性を掴むべき. public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* for(int i = 2; i <= 10000; i++){ final int sim_value = simulate(i); if(sim_value > 0){ final int big_value = smart(BigInteger.valueOf(i)); if(sim_value != big_value){ System.out.printf("%05d : %d, %d\n", i, sim_value, big_value); } } } */ BigInteger N = new BigInteger(sc.next()); System.out.println(smart(N)); } }