#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
int n, ans;
int f[maxn];
int main() {
  for (int i = 0; i < maxn; i++) f[i] = 0;
  cin >> 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;
    } else
      f[i] = i - f[i] + 1;
  }
  for (int i = f[n]; i <= n; i++) ans = min(ans, f[i]);
  cout << ans << endl;
  return 0;
}
