#include <bits/stdc++.h>
using namespace std;
int f[1000010];
int main() {
  int n;
  cin >> n;
  int ans = n;
  for (int i = 2; i <= n; i++) {
    if (!f[i]) {
      for (int j = 2; i * j <= n; j++) {
        f[i * j] = i;
      }
    }
    f[i] = i - f[i] + 1;
  }
  for (int i = f[n]; i <= n; i++) {
    ans = min(ans, f[i]);
  }
  cout << ans << endl;
  return 0;
}
