#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long gf(long long n) {
  long long mp = -1;
  while (n % 2 == 0) {
    mp = 2;
    n /= 2;
  }
  for (int i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      mp = i;
      n /= i;
    }
  }
  if (n > 2) mp = n;
  return mp;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, ans = 0, mini = LLONG_MAX;
  cin >> n;
  ans = gf(n);
  for (int i = n - ans + 1; i <= n; i++) {
    long long temp = gf(i);
    if (i - temp > 0) mini = min(mini, i - temp + 1);
  }
  cout << mini;
  return 0;
}
