#include <bits/stdc++.h>
using namespace std;
inline void solve() {
  long long n;
  cin >> n;
  vector<bool> isp(n + 1, 1);
  vector<long long> maxx(n + 1, -1);
  isp[0] = isp[1] = 0;
  for (long long i = 2; i <= n; i++) {
    if (isp[i]) {
      if (i * 2 <= n) {
        for (long long j = i * 2; j <= n; j += i) {
          isp[j] = 0;
          maxx[j] = max(maxx[j], i);
        }
      }
    }
  }
  long long best = 1, now = n;
  for (long long j = 2; j * j <= n; j++) {
    while (now % j == 0) {
      now /= j;
      best = max(best, j);
    }
  }
  best = max(best, now);
  long long ans = n - best + 1;
  for (long long i = n - best + 1; i < n; i++) {
    ans = min(ans, i - maxx[i] + 1);
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
