#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long N = 1111111;
  vector<long long> p;
  long long n;
  cin >> n;
  bool prime[n + 1];
  for (long long i = 0; i < n + 1; i++) prime[i] = true;
  prime[0] = prime[1] = false;
  for (long long i = 2; i < n + 1; i++) {
    if (!prime[i]) continue;
    p.push_back(i);
    for (long long j = i * i; j < n + 1; j += i) prime[j] = false;
  }
  long long dp[n + 1];
  for (long long i = 0; i < n + 1; i++) dp[i] = LLONG_MAX;
  for (long long i = 2; i < n + 1; i++) {
    if (prime[i]) continue;
    long long key = i;
    for (long long j = 0; p[j] <= sqrt(i); j++) {
      long long mul = i / p[j];
      mul--;
      if (key % p[j])
        continue;
      else
        dp[i] = min(dp[i], mul * p[j] + 1);
      while (key % p[j] == 0) key /= p[j];
    }
    if (key > 1) {
      long long mul = i / key;
      mul--;
      dp[i] = min(dp[i], mul * key + 1);
    }
  }
  long long res = LLONG_MAX;
  for (long long i = 0; i < p.size(); i++) {
    if (n % p[i]) continue;
    long long mul = n / p[i];
    mul--;
    long long l = mul * p[i] + 1;
    long long r = n;
    for (long long j = l; j < r + 1; j++) {
      res = min(res, dp[j]);
    }
  }
  cout << res << endl;
}
