#include <bits/stdc++.h>
using namespace std;
long long i, j, n, s, a[1000009];
int main() {
  cin >> n;
  for (i = 2; i <= n; i++) {
    if (!a[i])
      for (j = i * 2; j <= n; j += i) a[j] = i;
    a[i] = i - a[i] + 1;
  }
  s = n;
  for (i = a[n]; i <= n; i++) s = min(s, a[i]);
  cout << s;
}
