#include <bits/stdc++.h>
using namespace std;
bool p[1000010];
int ser[1000010];
void prime() {
  memset(p, 1, sizeof(p));
  p[1] = p[0] = 0;
  for (int i = 2; i < 1000010; i++) {
    if (p[i]) {
      ser[i] = 1;
      for (int j = i + i; j < 1000010; j += i) {
        p[j] = 0;
        ser[j] = i;
      }
    }
  }
}
int main() {
  int x2;
  prime();
  while (scanf("%d", &x2) != EOF) {
    int ans = 0x3f3f3f3f;
    for (int i = x2 - ser[x2] + 1; i < x2; i++) {
      ans = min(ans, i - ser[i] + 1);
    }
    if (ans == 0x3f3f3f3f) ans = x2 - 1;
    printf("%d\n", ans);
  }
  return 0;
}
