#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
int fac[maxn];
void init() {
  int i, j;
  for (i = 2; i < maxn; i++) {
    if (!fac[i]) {
      j = i;
      while (j < maxn) {
        fac[j] = i;
        j = j + i;
      }
    }
  }
}
int main() {
  int i, j, n, m, t, z;
  init();
  scanf("%d", &n);
  int x = fac[n];
  int ans = INF;
  for (i = n - x + 1; i <= n; i++) {
    int j = fac[i];
    if (j != i) ans = min(ans, i - j + 1);
  }
  printf("%d\n", ans);
  return 0;
}
