#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int n, ans = 0x3f3f3f3f, tot = 0, prime[1000010], a[1000010];
bool notprime[1000010];
void getprime() {
  notprime[1] = 1;
  for (int i = 2; i <= 1000000; ++i) {
    if (!notprime[i]) prime[++tot] = i, a[i] = i;
    for (int j = 1; prime[j] * i <= 1000000; ++j) {
      notprime[prime[j] * i] = 1;
      a[prime[j] * i] = a[i];
      if (i % prime[j] == 0) break;
    }
  }
}
int main() {
  int n = read(), x = n, mx = 0;
  getprime();
  for (int i = 2; i * i <= n; ++i) {
    if (x % i) continue;
    while (x % i == 0) x /= i;
    mx = i;
  }
  if (x != 1) mx = x;
  for (int i = n - mx + 1; i <= n; ++i) {
    if (!notprime[i]) continue;
    ans = min(ans, i - a[i] + 1);
  }
  printf("%d\n", ans);
  return 0;
}
