#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<char> prime(n + 1, true);
  vector<int> pr;
  prime[0] = prime[1] = false;
  for (int i = 2; i <= n; ++i)
    if (prime[i])
      if (i * 1ll * i <= n)
        for (int j = i * i; j <= n; j += i) prime[j] = false;
  for (int i = 2; i <= n; ++i)
    if (prime[i]) pr.push_back(i);
  int maxn = 0;
  for (int i = 0; i < pr.size(); ++i) {
    if (n % pr[i] == 0) maxn = pr[i];
  }
  int test = n - maxn + 1;
  int ans = test;
  for (int i = 0; i < pr.size(); ++i) {
    if (2 * pr[i] >= n) break;
    int x = test / pr[i];
    if (test % pr[i] != 0) x++;
    if (x * pr[i] > n) continue;
    if (x * pr[i] + 1 - pr[i] < ans) ans = x * pr[i] + 1 - pr[i];
  }
  cout << ans;
  return 0;
}
