#include <bits/stdc++.h>
using namespace std;
int x2, n, mp;
bool p[1001000];
vector<int> primes;
int lp[1001000];
int ans;
int md[1001000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int last_prime = 0;
  for (int i = 2; i <= 1000000; i++) {
    lp[i] = last_prime;
    if (!p[i]) {
      last_prime = i;
      primes.push_back(i);
      for (int j = i; j <= 1000000; j += i) {
        p[j] = true;
        md[j] = i;
      }
    }
  }
  cin >> x2;
  ans = x2 + 1;
  for (int i : primes) {
    if (i > x2) break;
    if (x2 % i == 0)
      for (int j = x2 - i + 1; j <= x2; j++) {
        if (md[j] == j) continue;
        ans = min(ans, j - md[j] + 1);
      }
  }
  cout << ans << endl;
  return 0;
}
