#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
template <typename T>
void print(const T& t, ostream& o) {
  if (!t.size()) return;
  o << "{" << t[0];
  for (int i = 1; i < t.size(); i++) o << ", " << t[i];
  o << "}" << endl;
}
template <typename T>
void pprint(const T& t, ostream& o) {
  if (!t.size()) return;
  o << "{" << t[0].first << "=" << t[0].second;
  for (int i = 1; i < t.size(); i++)
    o << ", " << t[i].first << "=" << t[i].second;
  o << "}" << endl;
}
int d(int n) {
  int x = 1;
  for (int i = 2; i < sqrt(n) + 1; i++) {
    while (n % i == 0) {
      n /= i;
      x = i;
    }
  }
  return max(x, n);
}
int main() {
  int x2;
  cin >> x2;
  int bound = d(x2);
  int ans = x2;
  for (int x1 = x2 - bound + 1; x1 < x2 + 1; x1++) {
    int p = d(x1);
    if (p != 1 && p != x1) {
      ans = min(ans, x1 - p + 1);
    }
  }
  cout << ans << endl;
}
