#include <bits/stdc++.h>
using namespace std;
long long int fct(long long int n) {
  long long int tmp = n;
  long long int m = 0;
  long long int i;
  for (i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      m = max(m, i);
      while (n % i == 0) {
        n /= i;
      }
    }
  }
  if (n != 1 && n != tmp) {
    m = max(m, n);
  }
  long long int c;
  if (m == 0) {
    return tmp;
  }
  c = tmp / m;
  n = (c - 1) * m + 1;
  return n;
}
void solve() {
  long long int n, i, m = 0, q = 0, mx = INT_MIN, mn = INT_MAX, k = 0, c = 0,
                      j = 0, d = 0, t = 0, sm = 0, pdt = 1;
  cin >> n;
  m = fct(n);
  mn = m;
  for (i = m; i < n; i++) {
    mn = min(mn, fct(i));
  }
  cout << mn << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
