#include <bits/stdc++.h>
using namespace std;
const int Nmax = 1000000 + 5;
int a;
int mn = 1 << 30, d, as, ans;
int lf(int a) {
  d = 2;
  as = a;
  ans = 0;
  while (d * d <= a) {
    if (a % d == 0) {
      ans = max(ans, d);
      while (a % d == 0) a /= d;
    }
    ++d;
  }
  if (a != as) ans = max(ans, a);
  return ans;
}
int main() {
  cin >> a;
  int b = (a / lf(a) - 1) * lf(a) + 1;
  for (int i = b; i < a; ++i)
    if (lf(i) != 0 && (i / lf(i) - 1) * lf(i) + 1 < mn)
      mn = min(mn, (i / lf(i) - 1) * lf(i) + 1);
    else if (lf(i) == 0)
      mn = min(mn, i);
  cout << mn;
  return 0;
}
