#include <bits/stdc++.h>
using namespace std;
bool prime(int a) {
  for (int b = 2; b < a; b += 2) {
    if (a % b == 0) return false;
    if ((b + 1) * (b + 1) > a) return true;
    if (b == 2) b--;
  }
}
int main() {
  while (1) {
    int a;
    cin >> a;
    if (a == 0) break;
    if (a % 2 == 0) {
      for (int b = a - 1;; b -= 2) {
        if (prime(b) && prime(b - 2) && prime(b - 6) && prime(b - 8)) {
          cout << b << endl;
          break;
        }
      }
    } else {
      for (int b = a;; b -= 2) {
        if (prime(b) && prime(b - 2) && prime(b - 6) && prime(b - 8)) {
          cout << b << endl;
          break;
        }
      }
    }
  }
}
