#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
int lp[N + 1];
int maxPrimeFactor(int t) {
  int mx = 0;
  while (t > 1) {
    mx = lp[t];
    t /= lp[t];
  }
  return mx;
}
int main() {
  std::ios::sync_with_stdio(false);
  vector<int> pr;
  for (int i = 2; i <= N; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j)
      lp[i * pr[j]] = pr[j];
  }
  int x2;
  cin >> x2;
  if (lp[x2] == x2) {
    cout << x2 << endl;
    return 0;
  }
  int mx = maxPrimeFactor(x2);
  int x1L = mx * ((x2 / mx) - 1) + 1;
  x1L = max(x1L, 3);
  int mn = x2;
  for (int i = x1L; i <= x2; i++) {
    if (lp[i] == i) {
      mn = min(mn, i);
      continue;
    }
    set<int> prms;
    int t = i;
    while (t > 1) {
      prms.insert(lp[t]);
      t /= lp[t];
    }
    for (int p : prms) {
      int x0L = p * ((i / p) - 1) + 1;
      x0L = max(x0L, 3);
      mn = min(mn, x0L);
    }
  }
  cout << mn << endl;
  return 0;
}
