#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long mxN = 1e6 + 1;
long long bprime[mxN], n;
void soe() {
  fill_n(bprime, mxN, 1);
  long long i = 2;
  while (i < mxN) {
    if (bprime[i] == 1)
      for (long long k = 2 * i; k < mxN; k += i) {
        bprime[k] = i;
      }
    i++;
  }
}
void solve() {
  soe();
  cin >> n;
  long long res = n;
  for (long long i = 0; i < bprime[n]; i++) {
    long long k = n - i;
    long long p = bprime[k];
    long long ans = max(3LL, k - p + 1);
    res = min(res, ans);
  }
  cout << res << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long T = 1, t = 0;
  while (t++ < T) {
    solve();
  }
}
