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