#include <bits/stdc++.h>
using namespace std;
bool isp(int a) {
  if (a < 2) return 0;
  for (int i = 2; i * i <= a; i++)
    if (a % i < 1) return 0;
  return 1;
}
int main() {
  int n;
  while (cin >> n, n) {
    if (n < 19)
      cout << 13 << endl;
    else {
      n = (n - 9) / 10 * 10 + 9;
      while (!isp(n) || !isp(n - 2) || !isp(n - 6) || !isp(n - 8)) n -= 10;
      cout << n << endl;
    }
  }
}
