#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e6 + 5;
const long long int mod = 1e9;
const long long int bmask = (1 << 17) + 1;
long long int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
long long int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
long long int prime[N];
long long int dp[N];
void primes() {
  prime[1] = 1;
  for (long long int i = 2; i < N - 2; i++) {
    if (prime[i] == 0) {
      prime[i] = i;
    }
  }
  for (long long int i = 2; i * i <= N - 3; i++) {
    if (prime[i] == i) {
      for (long long int j = i * i; j <= N - 3; j += i) {
        prime[j] = min(i, prime[j]);
      }
    }
  }
}
long long int X1(long long int x1) {
  vector<long long int> v;
  for (long long int x = x1; x != 1; x /= prime[x]) {
    v.push_back(prime[x]);
  }
  long long int ret = x1;
  for (long long int i = 0; i < v.size(); i++) {
    long long int x = v[i];
    ret = min(ret, x1 - x + 1);
  }
  return dp[x1] = ret;
}
long long int X2(long long int x2) {
  vector<long long int> v;
  for (long long int x = x2; x != 1; x /= prime[x]) {
    v.push_back(prime[x]);
  }
  long long int ret = x2;
  for (long long int i = 0; i < v.size(); i++) {
    long long int x = v[i];
    for (long long int j = x2 + 1 - 1; j >= x2 - x + 1; j--) {
      if (prime[j] == j) continue;
      ret = min(ret, X1(j));
    }
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  primes();
  memset(dp, -1, sizeof(dp));
  long long int x2;
  cin >> x2;
  long long int ans = 0;
  ans = X2(x2);
  cout << ans << endl;
  return 0;
}
