import java.util.*; import java.math.*; class Main{ static int small[]={0,0,0,0,3,0,5,0,7,7,7,0,11,0,13,7,7,0,8,0,19,19,7,0,23,23}; public static void main(String[] args){ Scanner s=new Scanner(System.in); BigInteger bi=s.nextBigInteger(); if(bi.compareTo(new BigInteger("26"))==-1){ System.out.println(small[bi.intValue()]); System.exit(0); } if(bi.mod(new BigInteger("8")).equals(BigInteger.ONE)&&bi.subtract(new BigInteger("8")).isProbablePrime(100)){ if(bi.mod(new BigInteger("14")).equals(BigInteger.ONE)&&bi.subtract(new BigInteger("14")).isProbablePrime(100)){ System.out.println(17); }else{ System.out.println(14); } }else{ System.out.println(8); } } }