#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  bool prime[10000000];
  fill(prime + 2, prime + 10000000, true);
  for (int i = 2; i < 10000000; ++i)
    if (prime[i])
      for (int j = i + i; j < 10000000; j += i) prime[j] = false;
  while (cin >> n && n) {
    for (int i = n; i < 10000000; ++i)
      if (prime[i - 8] && prime[i - 6] && prime[i - 2] && prime[i]) {
        cout << n << endl;
        break;
      }
  }
  return 0;
}
