#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

bool prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i < n; i++) {
        if ((i & 1) == 0 && i != 2) continue;
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    ifstream in("DATA2.txt");
    ofstream out("OUT2.txt");
    
    for (int i = 0; i < 5; i++) {
        int num;
        in >> num;
        int high, high2, low, low2;
        for (high = num+1; !prime(high); high++);
        for (high2 = high+1; !prime(high2); high2++);
        for (low = num-1; !prime(low) && (low > 0); low--);
        for (low2 = low-1; !prime(low2) && (low2 > 0); low2--);
        if (low2 <= 1 || (high2 - num) <= (num - low2)) {
            out << high2 << endl;
        } else {
            out << low2 << endl;
        }
    }
    
    return 0;
}

