#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
T gcd(T a, T b) {
  return ((b == 0) ? a : gcd(b, a % b));
}
vector<bool> prime;
vector<long long> primes;
void sieve(long long n) {
  prime.assign(n + 1, 1);
  prime[1] = false;
  for (long long i = 2; i <= n; i++) {
    if (prime[i]) {
      primes.push_back(i);
      for (long long j = i * 2; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }
}
int ans = 1e9;
void solve(int n, int cnt) {
  if (cnt == 2) {
    assert(n >= 3);
    ans = min(n, ans);
    return;
  }
  vector<int> div;
  vector<int> divp;
  for (int i = 1; (i * i) <= n; i++) {
    if (n % i == 0) {
      if (i != n) {
        div.push_back(i);
        if (prime[i]) divp.push_back(i);
      }
      if ((n / i) != i && (n / i) != n) {
        div.push_back(n / i);
        if (prime[n / i]) divp.push_back(n / i);
      }
    }
  }
  for (int i = 0; i < (int)(divp.size()); i++)
    for (int j = (int)(1); j < (int)(101); j++) {
      if (j > divp[i]) break;
      solve(n - divp[i] + j, cnt + 1);
    }
  solve(n, cnt + 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  sieve(1e6);
  int n;
  cin >> n;
  solve(n, 0);
  cout << ans << endl;
  return 0;
}
