#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
bool Finish_read;
template <class T>
inline void read(T &x) {
  Finish_read = 0;
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    if (ch == EOF) return;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  x *= f;
  Finish_read = 1;
}
template <class T>
inline void print(T x) {
  if (x / 10 != 0) print(x / 10);
  putchar(x % 10 + '0');
}
template <class T>
inline void writeln(T x) {
  if (x < 0) putchar('-');
  x = abs(x);
  print(x);
  putchar('\n');
}
template <class T>
inline void write(T x) {
  if (x < 0) putchar('-');
  x = abs(x);
  print(x);
}
const int maxn = 1000001;
int prime[maxn], cnt, n, a[maxn];
bool notprime[maxn];
int main() {
  notprime[1] = 1;
  for (int i = 2; i < maxn; i++) {
    if (!notprime[i]) prime[++cnt] = i;
    for (int j = 1; j <= cnt && prime[j] * i < maxn; j++) {
      notprime[prime[j] * i] = 1;
      if (i % prime[j] == 0) break;
    }
  }
  read(n);
  int ans = n;
  for (int i = 1; i <= cnt; i++)
    for (int j = prime[i] + prime[i]; j < maxn; j += prime[i])
      a[j] = j - prime[i] + 1;
  for (int i = 1; i <= cnt; i++) {
    if (n % prime[i] != 0) continue;
    for (int j = n - prime[i] + 1; j <= n; j++)
      if (notprime[j]) ans = min(ans, a[j]);
  }
  printf("%d\n", ans);
}
