#include <bits/stdc++.h>
using namespace std;
vector<int> valoresPrimos(int n) {
  vector<int> vec;
  vec.push_back(2);
  for (int i = 3; i <= n; i += 2) {
    bool primo = true;
    for (int j = 3; j * j <= i; j += 2)
      if (i % j == 0) {
        primo = false;
        break;
      }
    if (primo) vec.push_back(i);
  }
  return vec;
}
int factorMax(int a, vector<int> &primos) {
  for (int i : primos) {
    if (i * i > a) break;
    while (a % i == 0) a /= i;
    if (a == 1) return i;
  }
  return a;
}
int main() {
  int x2;
  scanf("%d", &x2);
  vector<int> vector = valoresPrimos(int(sqrt(x2)));
  int res = x2;
  int valormin = x2;
  int mcmX2 = factorMax(x2, vector);
  int valorMinx1 = mcmX2 * (x2 / mcmX2 - 1) + 1;
  for (int x1 = valorMinx1; x1 <= x2; x1++) {
    int mpf = factorMax(x1, vector);
    if (mpf == x1) continue;
    int y = x1 / mpf;
    int x0 = mpf * (y - 1) + 1;
    if (x0 < res) res = x0;
    if (y == 2) x1 = x2 + 1;
  }
  printf("%d\n", res);
}
