#include <bits/stdc++.h>
using namespace std;
int n, minx0 = INT_MAX;
int findPDiv(int n, bool first) {
  int n2 = n;
  int maxP;
  if (n % 2 == 0) maxP = 2;
  while (n % 2 == 0) n >>= 1;
  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0) maxP = i;
    while (n % i == 0) n /= i;
  }
  if (n > 2) maxP = n;
  if (maxP == n2) return INT_MAX;
  if (first)
    return maxP;
  else
    return n2 - maxP + 1;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  int maxP = findPDiv(n, 1);
  for (int i = n; i >= n - maxP + 1; i--) {
    minx0 = min(minx0, findPDiv(i, 0));
  }
  cout << minx0;
  return 0;
}
