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