#include <bits/stdc++.h>
using namespace std;
void qmax(int &x, int y) {
  if (x < y) x = y;
}
void qmin(int &x, int y) {
  if (x > y) x = y;
}
inline int read() {
  char s;
  int k = 0, base = 1;
  while ((s = getchar()) != '-' && s != EOF && !(isdigit(s)))
    ;
  if (s == EOF) exit(0);
  if (s == '-') base = -1, s = getchar();
  while (isdigit(s)) {
    k = k * 10 + (s ^ '0');
    s = getchar();
  }
  return k * base;
}
inline void write(int x) {
  static char cnt, num[15];
  cnt = 0;
  if (!x) {
    putchar('0');
    return;
  }
  for (; x; x /= 10) num[++cnt] = x % 10;
  for (; cnt; putchar(num[cnt--] + 48))
    ;
}
int n;
int vis[1001000];
int Max[1000000 + 100];
bool pd(int x) {
  if (vis[x] != 0) return vis[x] == 1;
  vis[x] = 2;
  for (int j = 2; j * j <= x; j++) {
    if (x % j == 0) return false;
  }
  vis[x] = 1;
  Max[x] = x;
  return true;
}
int ans;
int main() {
  n = read();
  ans = n;
  Max[1] = 1;
  for (int i = 2; i <= n; i++)
    if (Max[i] == 0) {
      for (int j = i; j <= n; j += i) {
        Max[j] = i;
      }
    }
  for (int i = n - Max[n] + 1; i <= n; i++) {
    if (i - Max[i] + 1 < Max[i]) continue;
    qmin(ans, i - Max[i] + 1);
  }
  printf("%d\n", ans);
  return 0;
}
