#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 1005;
int n;
bool prime[maxn];
int sp[maxn];
int res = 1 << 30;
void solve(int n) {
  vector<int> primes;
  int tmp = n;
  while (sp[tmp] != tmp) {
    primes.push_back(sp[tmp]);
    tmp /= sp[tmp];
  }
  if (tmp != 1) primes.push_back(tmp);
  for (int i = 0; i < ((int)((primes).size())); i++) {
    int p = primes[i];
    for (int j = max(3, n - p + 1); j <= n; ++j) {
      int curr = j;
      while (sp[curr] != curr) {
        res = min(res, j - sp[curr] + 1);
        curr /= sp[curr];
      }
      if (curr != 1 && curr != j) {
        res = min(res, j - sp[curr] + 1);
      }
    }
  }
}
int main() {
  sp[1] = 1;
  scanf("%d", &n);
  memset(prime, 1, sizeof(prime));
  memset(sp, -1, sizeof(sp));
  for (int i = 2; i <= n; ++i) {
    if (prime[i]) {
      sp[i] = i;
      for (int j = i << 1; j <= n; j += i) {
        if (sp[j] == -1) sp[j] = i;
        prime[j] = false;
      }
    }
  }
  solve(n);
  cout << res << "\n";
}
