// The "File_example" class.
import java.awt.*;
import java.io.*; //must import file package
public class P2
{
    public static void main (String[] args) throws IOException  //must have throws IOException to make files works
    {
	PrintWriter pw = new PrintWriter (new FileWriter ("OUT2.txt")); //declares an object that will print to a file
	BufferedReader br = new BufferedReader (new FileReader ("DATA2.txt")); //declares an object that will read from a file
	String str = "";

	int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107};
	int prime2[] = new int [108];
	int temp;
	for (int i = 0 ; i < 28 ; i++)
	{
	    temp = prime [i];
	    prime2 [temp] = temp;
	    //System.out.println (prime2 [temp]);
	}
	str = br.readLine (); //read from file, data type read in is string only
	int num;
	int temp2;


	int rightfirst = 0;
	int leftfirst = 0;
	int rightsecond = 0;
	int leftsecond = 0;
	while (str != null)
	{
	    // String[] num = str.split (" "); //if there are one line with multiple inputs, str.split(" ") will split the string into how many
	    //ever array elements that are separated by " "(in this case a space)
	    int count1 = 1;
	    int count2 = 1;
	    int count3 = 1;
	    int count4 = 1;
	    num = Integer.parseInt (str);

	    do
	    {
		rightfirst = prime2 [num + count1];

		count1++;


	    }
	    while (rightfirst == 0);

	    do
	    {
		leftfirst = prime2 [num - count2];
		count2++;

	    }
	    while (leftfirst == 0);


	    do
	    {
		rightsecond = prime2 [rightfirst + count3];
		count3++;
	    }
	    while (rightsecond == 0);
	    do
	    {

		leftsecond = prime2 [leftfirst - count4];
		count4++;

	    }
	    while (leftsecond == 0);




	    if (Math.abs (leftsecond - num) >= Math.abs (rightsecond - num))
		pw.println (rightsecond);
	    if (Math.abs (leftsecond - num) < Math.abs (rightsecond - num))
		pw.println (leftsecond);







	    str = br.readLine ();
	}



	pw.close (); //close file, make sure all output are included in output file
	br.close ();
	// Place your program here.  'c' is the output console
    } // main method
} // File_example class

