#include <bits/stdc++.h>
using namespace std;
int ans = 1e9;
int f[1000010];
int main() {
  int n, i, j;
  scanf("%d", &n);
  for (i = 2; i <= n; i++) {
    if (!f[i]) {
      for (j = 2 * i; j <= n; j += i) {
        f[j] = i;
      }
    }
    f[i] = i - f[i] + 1;
  }
  for (i = f[n]; i <= n; i++) {
    if (f[i] < ans) ans = f[i];
  }
  cout << ans << endl;
}
