#include <bits/stdc++.h>
int main() {
  int n;
  int a[10000000];
  int b;
  int i, j, p;
  int c[1000];
  int d[1000];
  p = 1;
  while (1 == 1) {
    scanf("%d", &c[p]);
    if (c[p] != 0)
      p++;
    else
      break;
  }
  b = (int)pow(10000000, 0.5);
  a[0] = -1;
  for (i = 2; i <= b; i++) {
    for (j = 2; j <= 10000000 / i; j++) {
      a[i * j - 1] = -1;
    }
  }
  for (i = 1; i < p; i++) {
    for (j = c[i]; j > 0; j--) {
      if (a[j - 1] != -1) {
        if (a[j - 2 - 1] != -1 && a[j - 6 - 1] != -1 && a[j - 8 - 1] != -1) {
          d[i] = j;
          j = 0;
        }
      }
    }
    printf("%d\n", d[i]);
  }
  return 0;
}
