#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, ans;
int f[maxn];
inline int Min(int x, int y) { return x < y ? x : y; }
int main() {
  scanf("%d", &n);
  ans = n;
  for (int i = 2; i <= n; i++) {
    if (!f[i]) {
      for (int j = 2 * i; j <= n; j += i) f[j] = i;
    }
    f[i] = i - f[i] + 1;
  }
  for (int i = f[n]; i <= n; i++) ans = Min(ans, f[i]);
  printf("%d\n", ans);
  return 0;
}
