#include <bits/stdc++.h>
int prime[10000001];
int main(void) {
  int n[1000], s = 0, max = 0;
  while (1) {
    scanf("%d", &n[s]);
    if (n[s] == 0) {
      break;
    }
    if (max < n[s]) {
      max = n[s];
    }
    s++;
  }
  double sqr = sqrt((double)max);
  for (int i = 2; i <= max; i++) {
    prime[i] = 0;
  }
  int front, back = 2, value[100000], suffix = 0;
  while (1) {
    for (int i = back; i <= max; i++) {
      if (prime[i] == 0) {
        front = i;
        value[suffix++] = front;
        break;
      }
    }
    if (front >= sqr) {
      break;
    }
    for (int i = front; i <= max; i += front) {
      prime[i] = 1;
    }
  }
  for (int i = front + 1; i <= max; i++) {
    if (prime[i] == 0) {
      prime[i] = 1;
      value[suffix++] = i;
    }
  }
  int point;
  for (int k = 0; k < s; k++) {
    point = 0;
    for (int i = 0; i < suffix; i++) {
      if (value[i] > n[k]) {
        point = i - 1;
        break;
      } else if (value[i] == n[k]) {
        point = i;
        break;
      }
    }
    if (point == 0) {
      point = suffix - 1;
    }
    for (int i = point; i >= 0; i--) {
      if (value[i] - value[i - 1] == 2) {
        if (value[i] - value[i - 2] == 6) {
          if (value[i] - value[i - 3] == 8) {
            printf("%d\n", value[i]);
            break;
          }
        }
      }
    }
  }
  return 0;
}
