#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
char s[550][550];
int com(int x) {
  int t = x;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      t = i;
      while (x % i == 0) x /= i;
    }
  }
  if (x != 1) t = x;
  return t;
}
int main() {
  int n, m, t;
  scanf("%d", &n);
  t = com(n);
  int l = max((n / t - 1) * t + 1, t + 1), r = n, ans = n;
  for (int i = l; i <= r; i++) {
    t = com(i);
    ans = min(ans, max(t + 1, (i / t - 1) * t + 1));
  }
  printf("%d\n", ans);
  return 0;
}
