#include <bits/stdc++.h>
using namespace std;
bool h[1000010];
int find(int x2) {
  int i, p = 1, t = x2;
  for (i = 2; i <= sqrt(x2); ++i)
    if (t % i == 0) {
      p = i;
      while (t % i == 0) t /= i;
    }
  if (t > 1) p = t;
  return p;
}
int main() {
  int i, j, x1, x2, p, t, s, ans = 1e6;
  scanf("%d", &x2);
  p = find(x2);
  s = x2 - p + 1;
  for (i = 2; i <= x2; ++i)
    if (!h[i]) {
      for (j = i + i; j <= x2; j += i) h[j] = 1;
      for (j = max(((s - 1) / i + 1) * i, i + i); j <= x2; j += i)
        ans = min(ans, j - i + 1);
    }
  printf("%d\n", ans);
  return 0;
}
