#include <bits/stdc++.h>
using namespace std;
int lp[1000000 + 1];
int phi[1000000 + 1];
vector<int> pr;
void calc_sieve() {
  phi[1] = 1;
  for (int i = 2; i <= 1000000; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      phi[i] = i - 1;
      pr.push_back(i);
    } else {
      if (lp[i] == lp[i / lp[i]])
        phi[i] = phi[i / lp[i]] * lp[i];
      else
        phi[i] = phi[i / lp[i]] * (lp[i] - 1);
    }
    for (int j = 0;
         j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= 1000000; ++j)
      lp[i * pr[j]] = pr[j];
  }
}
int getmin(int a, int b) {
  int ans = 1000000007;
  for (auto i = (a); i < b + 1; i++) {
    int j = i;
    while (j != 1) {
      ;
      if (i != lp[j]) ans = min(ans, i - lp[j] + 1);
      int k = lp[j];
      while (j % k == 0) j = j / k;
    }
  }
  return ans;
}
int main() {
  int n, nn;
  cin >> n;
  nn = n;
  calc_sieve();
  int finans = 1000000007;
  while (nn != 1) {
    ;
    finans = min(finans, getmin(n - lp[nn] + 1, n));
    int k = lp[nn];
    while (nn % k == 0) nn = nn / k;
    ;
  }
  cout << finans << endl;
}
