#include <bits/stdc++.h>
using namespace std;
bool b[1000010];
int main() {
  int n, i, j;
  scanf("%d", &n);
  for (i = 2; i <= n; i++) {
    if (b[i]) continue;
    if (i * 1ll * i > n) continue;
    for (j = i * i; j <= n; j += i) b[j] = true;
  }
  vector<int> v;
  for (i = 2; i <= n; i++)
    if (n % i == 0 && b[i] == 0) v.push_back(i);
  int res = 1e9;
  for (i = 2; i <= n - v.back(); i++)
    if (b[i] == 0) {
      int x = (n - v.back()) / i + 1;
      if (x * i <= n) res = min(res, x * i - i + 1);
    }
  printf("%d\n", res);
  return 0;
}
