#include <bits/stdc++.h>
using namespace std;
bool prime[1000000];
const int MAX_N = 1000000;
int num[1000000];
int main() {
  prime[0] = prime[1] = false;
  for (int i = 2; i < MAX_N; i++) {
    prime[i] = true;
  }
  for (int i = 2; i * i < MAX_N; i++) {
    if (prime[i]) {
      for (int j = i * i; j < MAX_N; j += i) prime[j] = false;
    }
  }
  int cnt = 0, tmp = MAX_N;
  for (int i = 5; i < MAX_N - 8; i++) {
    if (prime[i] && prime[i + 2] && prime[i + 6] && prime[i + 8]) {
      if (i + 8 < tmp) tmp = i + 8;
    }
    num[i] = tmp;
  }
  int n, ans;
  while (true) {
    cin >> n;
    if (cin.eof()) break;
    int ans = 0;
    for (int i = 0; i < n; i++) ans = max(ans, num[n]);
    cout << ans << endl;
  }
  return 0;
}
