#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
template <class T>
T RoundOff(T a) {
  return int(a + .5 - (a < 0));
}
template <class T, class C>
void chmax(T& a, C b) {
  if (a < b) a = b;
}
template <class T, class C>
void chmin(T& a, C b) {
  if (b < a) a = b;
}
template <class T, class C>
pair<T, C> mp(T a, C b) {
  return make_pair(a, b);
}
vector<bool> getPrimeTable(const int& max) {
  vector<bool> table(max + 1);
  table[2] = true;
  int i, j;
  for (i = 3; i <= max; i += 2) {
    if (!(table[i] = i % 2 == 1)) continue;
    for (j = 3; j * j <= i; j += 2) {
      if (i % j == 0) {
        table[i] = false;
        break;
      }
    }
  }
  return table;
}
bool isPrime(int x) {
  if (x == 2) return true;
  if (x % 2 == 0 || x < 2) return false;
  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  vector<int> table(10000000, 2);
  int n;
  while (cin >> n && n) {
    int dx[] = {0, -2, -6, -8};
    if (n % 2 == 0) n--;
    for (int i = n; 8 <= i; i -= 2) {
      for (int j = 0; j < 4; j++) {
        int nn = i + dx[j];
        if (!(table[nn] == 1 || (table[nn] == 2 && (table[nn] = isPrime(nn)))))
          break;
        else if (j == 3) {
          cout << i << endl;
          goto ng;
        }
      }
    }
  ng:;
  }
}
