#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x;
  cin >> x;
  vector<long long int> prime;
  long long int n = x;
  if (n % 2 == 0) {
    prime.push_back(2);
    while (n % 2 == 0) n /= 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    if (n % i == 0) {
      prime.push_back(i);
      while (n % i == 0) n = n / i;
    }
  }
  if (n > 2) prime.push_back(n);
  sort(prime.begin(), prime.end());
  long long int great = prime[prime.size() - 1];
  long long int lo = ((x / great) - 1) * great;
  long long int ans = INT_MAX;
  for (long long int i = lo + 1; i < x; i++) {
    n = i;
    vector<long long int> v;
    if (n % 2 == 0) {
      v.push_back(2);
      while (n % 2 == 0) n /= 2;
    }
    for (int j = 3; j <= sqrt(n); j = j + 2) {
      if (n % j == 0) {
        v.push_back(j);
        while (n % j == 0) n = n / j;
      }
    }
    if (n > 2) v.push_back(n);
    sort(v.begin(), v.end());
    long long int val;
    if (v[v.size() - 1] > 1) {
      if ((((i / v[v.size() - 1]) - 1) * v[v.size() - 1]) > 0)
        val = ((i / v[v.size() - 1]) - 1) * v[v.size() - 1];
      else
        val = i - 1;
      ans = min(ans, val + 1);
    }
  }
  cout << ans << endl;
}
