#include <bits/stdc++.h>
using namespace std;
vector<long long> getprimes(long long n) {
  vector<long long> sieve(n);
  for (long long i = 2;; i++) {
    if (i > sieve.size()) break;
    if (sieve[i] != 0) continue;
    for (long long j = 2;; j++) {
      if (i * j > sieve.size()) break;
      sieve[i * j] = 1;
    }
  }
  vector<long long> ans;
  for (long long i = 2; i < sieve.size(); i++) {
    if (sieve[i] == 0) ans.push_back(i);
  }
  return ans;
}
vector<long long> primefactors(long long n) {
  vector<long long> ans;
  if (n % 2 == 0) {
    ans.push_back(2);
  }
  while (n % 2 == 0) {
    n = n / 2;
  }
  for (long long i = 3; i <= sqrt(n); i = i + 2) {
    if (n % i == 0) {
      ans.push_back(i);
    }
    while (n % i == 0) {
      n = n / i;
    }
  }
  if (n > 2) {
    ans.push_back(n);
  }
  return ans;
}
int32_t main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  vector<long long> primef = primefactors(n);
  sort(primef.rbegin(), primef.rend());
  long long x = n - primef[0] + 1;
  vector<long long> primes = getprimes(1000000);
  map<long long, long long> m;
  for (long long i = 0; i < primes.size(); i++) m[primes[i]]++;
  long long ans = LLONG_MAX;
  for (long long i = x; i <= n; i++) {
    long long val = i;
    if (m.count(val)) continue;
    vector<long long> pf = primefactors(val);
    for (long long j = 0; j < pf.size(); j++) {
      long long smallans = val - pf[j] + 1;
      if (smallans < ans) ans = smallans;
    }
  }
  cout << ans << "\n";
}
