#include <bits/stdc++.h>
void prec(long s);
long p[5000000], pc = 0;
int main(void) {
  long t, n, i, ans, i1, i2, i3, max = 0;
  int f;
  p[0] = 2;
  pc++;
  while (scanf("%d", &n) != EOF) {
    if (max == 0) {
      i = 3;
      max = n;
    } else if (n <= max)
      i = 10000000;
    else if (n > max) {
      i = max + 1;
      if (i % 2 == 0) i++;
    }
    for (; i <= 10000000; i += 2) {
      prec(i);
    }
    f = 0;
    for (t = 0;; t++) {
      if (p[t] > n) break;
    }
    for (i = t; f < 1; i--) {
      for (i1 = i - 1;; i1--) {
        if (p[i1] < p[i] - 2)
          break;
        else if (p[i] - 2 == p[i1]) {
          for (i2 = i1 - 1;; i2--) {
            if (p[i2] < p[i] - 6)
              break;
            else if (p[i2] == p[i] - 6) {
              for (i3 = i2 - 1;; i3--) {
                if (p[i3] < p[i] - 8)
                  break;
                else if (p[i3] == p[i] - 8) {
                  f = 1;
                  printf("%ld\n", p[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 (s % p[i] == 0) return;
    if (p[i] > sqrt(s)) break;
  }
  p[pc] = s;
  pc++;
  return;
}
