#include <bits/stdc++.h>
using namespace std;
const long long INF = 4000000000000000000LL;
const int nax = 1e6;
vector<int> prm;
vector<bool> np(nax + 1);
void sieve() {
  for (int i = 2; i <= nax; i++) {
    if (!np[i]) prm.push_back(i);
    for (int j = 0; j < prm.size() && 1LL * prm[j] * i <= nax; j++) {
      np[i * prm[j]] = true;
      if (i % prm[j] == 0) break;
    }
  }
}
int factorize(int n) {
  int mx = 1;
  for (int i = 0; i < prm.size() && prm[i] * prm[i] <= n; i++) {
    if (n % prm[i] == 0) {
      mx = max(mx, prm[i]);
      while (n % prm[i] == 0) {
        n /= prm[i];
      }
    }
  }
  if (n > 1) {
    mx = max(mx, n);
  }
  return mx;
}
int main() {
  sieve();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x2;
  cin >> x2;
  int p2 = factorize(x2);
  int range = (x2 / p2 - 1) * p2;
  int x0 = range + 1;
  for (int x1 = range + 1; x1 <= x2; x1++) {
    if (np[x1] == false) continue;
    int p1 = factorize(x1);
    int r = (x1 / p1 - 1) * p1;
    if (r < range) {
      x0 = min(x0, r + 1);
    }
  }
  cout << x0 << endl;
}
