#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 1e6;
const long long LINF = (long long)1e18 + 1e9;
const long double EPS = (long double)1e-7;
const long long MOD = (long long)1e9 + 7;
long long power(long long x, long long y, long long mod = MOD) {
  if (y == 0) {
    return 1;
  }
  if (y & 1) {
    return power(x, y - 1, mod) * x % mod;
  } else {
    long long tmp = power(x, y / 2, mod);
    return tmp * tmp % mod;
  }
}
template <typename A, typename B>
bool mini(A &x, const B &y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}
template <typename A, typename B>
bool maxi(A &x, const B &y) {
  if (y > x) {
    x = y;
    return true;
  }
  return false;
}
void add(long long &x, long long y) {
  x += y;
  if (x >= MOD) x -= MOD;
  if (x < 0) x += MOD;
}
void run();
int main() {
  if (strlen("")) {
    freopen(
        ""
        ".in",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  run();
  return 0;
}
const int N = (int)1e6 + 123;
bool isnp[N];
vector<int> divs[N];
int dp[N];
void run() {
  for (auto i = 2; i < N; ++(i)) {
    if (isnp[i]) continue;
    for (int j = i; j < N; j += i) {
      divs[j].push_back(i);
    }
    for (int j = i + i; j < N; j += i) {
      isnp[j] = 1;
    }
  }
  for (auto i = 0; i < N; ++(i)) dp[i] = INF;
  for (auto x = 3; x < N; ++(x)) {
    if (!isnp[x]) continue;
    for (int p : divs[x]) {
      mini(dp[x], max(3, x - p + 1));
    }
  }
  int n;
  cin >> n;
  int ans = INF;
  for (int p : divs[n]) {
    int L = n - p + 1;
    int R = n;
    for (auto i = L; i < R + 1; ++(i)) {
      mini(ans, dp[i]);
    }
  }
  cout << ans << "\n";
}
