#include <bits/stdc++.h>
using namespace std;
bool bz[1000100];
int zs[1000100], ma[1000100];
int min(int x, int y) { return x < y ? x : y; }
int main() {
  int n, m, i, j, k, mz, mzz, ans = 99999999;
  scanf("%d", &n);
  for (i = 2; i <= n; i++)
    if (!bz[i]) {
      zs[++zs[0]] = i;
      if (n % i == 0) mz = i;
      for (j = 2; j <= n / i; j++) bz[i * j] = 1, ma[i * j] = i;
    }
  i = mz * (n / mz - 1) + 1;
  for (; i <= n; i++)
    if (bz[i]) ans = min(ans, ma[i] * (i / ma[i] - 1) + 1);
  printf("%d\n", ans);
  return 0;
}
