#include <bits/stdc++.h>
using namespace std;
vector<int> V;
int check[1000010];
void Pre() {
  memset(check, 0, sizeof(check));
  for (int i = 2; i <= 1000000; ++i) {
    if (!check[i]) {
      for (int j = i + i; j <= 1000000; j += i) {
        check[j] = i;
      }
    }
  }
}
int main() {
  Pre();
  int X2;
  scanf("%d", &X2);
  int max_p = check[X2];
  int ans = 1e8;
  for (int i = X2 - max_p + 1; i <= X2; ++i) {
    if (check[i] == 0) continue;
    ans = min(ans, i - check[i] + 1);
  }
  printf("%d\n", ans);
  return 0;
}
