#include <bits/stdc++.h>
int check(long s);
void sosu(long s);
void prec(long s);
long f1 = 0, f2 = 0;
long so[100000], sc = 0;
long p[80000], pc = 0;
int main() {
  long n, i, c[4], max = 0;
  int l, f;
  while (scanf("%d", &n) != EOF) {
    f = 0;
    if (pc == 0 || n > max) {
      if (pc == 0)
        i = 2;
      else
        i = max + 1;
      for (; i <= n; i += 2) {
        prec(i);
        if (i % 2 == 0) i -= 1;
      }
    }
    for (i = pc - 1;; i--) {
      if (p[i] <= n) break;
    }
    if (max < n) max = n;
    for (c[0] = i; f != 1; c[0]--) {
      for (c[1] = c[0] - 1;; c[1]--) {
        if (p[c[1]] < p[c[0]] - 2)
          break;
        else if (p[c[1]] == p[c[0]] - 2) {
          for (c[2] = c[1] - 1;; c[2]--) {
            if (p[c[2]] < p[c[0]] - 6)
              break;
            else if (p[c[2]] == p[c[0]] - 6) {
              for (c[3] = c[2] - 1;; c[3]--) {
                if (p[c[3]] < p[c[0]] - 8)
                  break;
                else if (p[c[3]] == p[c[0]] - 8) {
                  printf("%d\n", p[c[0]]);
                  f = 1;
                }
              }
            }
          }
        }
      }
    }
  }
  return 0;
}
int check(long s) {
  long i;
  return 0;
}
void prec(long s) {
  long i;
  if (s % 2 == 0 && s != 2) return;
  i = sqrt(s);
  if (i * i == s) return;
  for (i = 0; i < pc; i++) {
    if (p[i] > sqrt(s)) break;
    if (s % p[i] == 0) return;
  }
  if (p[pc - 1] < s || pc == 0) {
    p[pc] = s;
    pc++;
  }
  return;
}
