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