#include <bits/stdc++.h>
long long INF = 1e9;
long long LINF = INF * INF;
long long mod = 1000000007;
using namespace std;
int n;
int lar[1000007];
int pre[1000007];
int pr[1000007];
int main() {
  for (int i = 2; i <= 1000000; i++)
    if (lar[i] == 0) {
      pr[i] = 1;
      for (int o = i; o <= 1000000; o += i) lar[o] = i;
    }
  cin >> n;
  for (int i = 2; i <= 1000000; i++) pre[i] = i - lar[i] + 1;
  int mn = 1000000;
  for (int i = pre[n]; i <= n; i++)
    if (!pr[i]) mn = min(mn, pre[i]);
  cout << mn << endl;
  return 0;
}
