#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> lp(n + 1);
  vector<int> pr;
  for (int i = 2; i <= n; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= n; ++j)
      lp[i * pr[j]] = max(pr[j], lp[i]);
  }
  int cur = n;
  int res = n;
  int prev = n;
  for (int p = lp[cur]; cur != 1; cur /= p, p = lp[cur]) {
    for (int c = n - p + 1; c < prev; ++c) {
      if (c != lp[c]) {
        if (c - lp[c] + 1 >= 3) {
          res = min(res, c - lp[c] + 1);
        } else {
          for (int c2 = max(c - lp[c] + 1, 3); c2 < c; ++c2) {
            res = min(res, c2);
            break;
          }
        }
      } else if (c >= 3) {
        res = min(res, c);
      }
    }
    prev = n - p + 1;
  }
  cout << res << "\n";
  return 0;
}
