#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 9;
const long long N = 500500;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long ans = INF;
  long long x2;
  cin >> x2;
  long long n = x2;
  long long pn = 0;
  for (long long i = 2; i <= sqrt(x2); i++) {
    while (x2 % i == 0) {
      pn = max(pn, i);
      x2 = x2 / i;
    }
  }
  if (x2 > 1) {
    pn = max(pn, x2);
  }
  for (long long x1 = n - pn + 1; x1 <= n; x1++) {
    long long num = x1, big = 0;
    for (long long i = 2; i <= sqrt(num); i++) {
      while (num % i == 0) {
        big = max(big, i);
        num = num / i;
      }
    }
    if (num > 1) {
      big = max(big, num);
    }
    if (x1 != big)
      ans = min(ans, x1 - big + 1);
    else
      ans = min(ans, x1);
  }
  cout << ans;
  return 0;
}
