#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long x2;
  cin >> x2;
  long long n = 1e6 + 10;
  vector<bool> prime(n + 1, true);
  vector<long long> t(n + 1);
  prime[0] = prime[1] = false;
  vector<long long> p(n + 1);
  p[0] = p[1] = 1;
  for (long long i = 2; i <= n; ++i)
    if (prime[i])
      if (i * 1ll * i <= n)
        for (long long j = i * i; j <= n; j += i) {
          prime[j] = false;
          p[j] = i;
        }
  for (long long i = 0; i <= n; i++) {
    if (prime[i])
      t[i] = 1;
    else {
      t[i] = max(p[i], t[i / p[i]]);
      if (prime[i / p[i]]) t[i] = max(t[i], i / p[i]);
    }
  }
  long long res = 1e9;
  for (long long i = max(3ll, x2 - t[x2] + 1); i <= x2; i++) {
    res = min(res, max(3ll, i - t[i] + 1));
  }
  cout << res;
  return 0;
}
