#include <bits/stdc++.h>
const double eps = 1e-8;
const double pi = acos(-1.0);
const long long N = 1e6 + 50;
const long long M = 20100403;
const long long inf = 0x3f3f3f3f;
const long long INF = 0x7fffffffffffffffLL;
using namespace std;
long long n, m;
long long T;
long long ans;
long long vis[N], prime[N], cnt;
long long maxn[N];
void init() {
  vis[1] = 1;
  for (long long i = 2; i < N; i++) {
    if (!vis[i]) {
      prime[cnt++] = i;
      maxn[i] = i;
    }
    for (long long j = 0; j < cnt && prime[j] * i < N; j++) {
      vis[prime[j] * i] = 1;
      maxn[prime[j] * i] = maxn[i];
      if (i % prime[j] == 0) {
        break;
      }
    }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin >> n;
  init();
  long long p = 0;
  for (long long i = cnt - 1; i >= 0; i--) {
    if (n % prime[i] == 0) {
      p = i;
      break;
    }
  }
  long long t = n - prime[p] + 1;
  long long ans = INF;
  for (long long i = t; i <= n; i++) {
    if (vis[i]) ans = min(ans, (i / maxn[i] - 1) * maxn[i] + 1);
  }
  cout << ans << "\n";
  return 0;
}
