#include <bits/stdc++.h>
using namespace std;
int lpd(int n) {
  int ans = -1;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans = i;
      while (n % i == 0) n /= i;
    }
  }
  if (n > 1) ans = n;
  return ans;
}
int x, p[1000006], pd, t, ans;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  p[0] = p[1] = 1;
  for (int i = 2; i * i <= 1e6; i++) {
    if (p[i] == 0) {
      for (int j = i * i; j <= 1e6; j += i) {
        p[j] = 1;
      }
    }
  }
  cin >> x;
  ans = 1e9;
  pd = lpd(x);
  t = x / pd;
  t -= 1;
  if (p[pd * t + 1] == 0 && (pd * t + 2 == x)) {
    cout << (x - 1) << "\n";
    return 0;
  }
  for (int i = pd * t + 1; i < x; i++) {
    if (p[i] == 0) continue;
    pd = lpd(i);
    t = i / pd;
    t -= 1;
    ans = min(ans, pd * t + 1);
  }
  cout << ans << "\n";
}
