#include <bits/stdc++.h>
int main() {
  int num, h, i, j, k, maxprime, *n;
  n = (int *)malloc(sizeof(int) * 10000001);
  if (n == NULL) {
    exit(0);
  };
  for (h = 2; h < 10000001; h += 1) {
    n[h] = h;
  };
  for (i = 2; i < 10000001; i += 1) {
    for (j = n[i] * 2; j < 10000001; j += n[i]) {
      if (n[i] == 0) {
        break;
      };
      n[j] = 0;
    };
  };
  while (scanf("%d", &num) != EOF) {
    if (num == 0) {
      break;
    };
    for (k = num; k > 1; k -= 1) {
      if (n[k] != 0) {
        maxprime = n[k];
        break;
      };
    };
    printf("%d\n", maxprime);
  };
  free(n);
  return 0;
}
