#include <bits/stdc++.h>
using namespace std;
int maxp[1000001];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  for (int i = 2; i <= n; i += 1)
    if (maxp[i] == 0)
      for (int j = i + i; j <= n; j += i) maxp[j] = i;
  int ans = n;
  for (int i = n - maxp[n] + 1; i <= n; i += 1) ans = min(ans, i - maxp[i] + 1);
  cout << ans;
  return 0;
}
