#include <bits/stdc++.h>
const double eps = 1e-10;
const float epsf = 1e-6;
using namespace std;
inline long long int __gcd(long long int a, long long int b) {
  if (a == 0 || b == 0) {
    return max(a, b);
  }
  long long int tempa, tempb;
  while (1) {
    if (a % b == 0)
      return b;
    else {
      tempa = a;
      tempb = b;
      a = tempb;
      b = tempa % tempb;
    }
  }
}
inline int compfloat(float& x, float& y) {
  if (fabs(x - y) < epsf)
    return 0;
  else if (x - y > 0)
    return 1;
  return -1;
}
inline int compdouble(double x, double y) {
  if (fabs(x - y) < eps)
    return 0;
  else if (x - y > 0)
    return 1;
  else
    return -1;
}
bool prime(long long int k) {
  for (long long int i = 2; i * i <= k; i++)
    if (k % i == 0) {
      return false;
    }
  return true;
}
void pdash(int n = 1) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 30; j++) {
      cout << "-";
    }
    cout << "\n";
  }
}
vector<int> SieveOfEratosthenes(int n) {
  list<int> l;
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p]) l.insert(l.begin(), p);
  vector<int> vec(l.begin(), l.end());
  return vec;
}
void solve() {
  int n;
  cin >> n;
  vector<int> prime = SieveOfEratosthenes(n);
  vector<int> p(n + 1, 1);
  reverse(prime.begin(), prime.end());
  p[0] = 0;
  p[1] = 1;
  for (auto& j : prime) {
    long long int k = 1;
    while (j * k <= n) {
      p[j * k] = j;
      k++;
    }
  }
  for (int i = 0; i <= n; i++) {
    if (p[i] == 1) p[i] = i;
  }
  int ans = numeric_limits<int>::max();
  for (int i = n - p[n] + 1; i <= n; i++) {
    if (i == p[i]) continue;
    ans = min(ans, i - p[i] + 1);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
