#include <bits/stdc++.h>
using namespace std;
int n, flag[1000010], tot, a[1000010], prime[1000010];
int main() {
  scanf("%d", &n);
  flag[1] = 1;
  for (int i = 2; i <= n; ++i) {
    if (!flag[i]) prime[++tot] = a[i] = i;
    for (int j = 1; j <= tot && i * prime[j] <= n; ++j) {
      flag[i * prime[j]] = 1, a[i * prime[j]] = a[i];
      if (i % prime[j] == 0) break;
    }
  }
  int ans = n;
  for (int i = n - a[n] + 1; i <= n; ++i)
    if (flag[i]) ans = min(ans, i - a[i] + 1);
  printf("%d\n", ans);
  return 0;
}
