#include <bits/stdc++.h>
using namespace std;
long long const MOD = 1e9 + 7;
vector<int> pr;
vector<int> factorize(int x) {
  vector<int> factors;
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0) {
      factors.push_back(i);
    }
    while (x % i == 0) {
      x /= i;
    }
  }
  if (x != 1) {
    factors.push_back(x);
  }
  return factors;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int ans = 1e7;
  vector<int> d = factorize(n);
  for (int i = 0; i < d.size(); i++) {
    int forx1 = n - d[i];
    for (int j = forx1 + 1; j < n; j++) {
      ans = min(j, ans);
      vector<int> dforx1 = factorize(j);
      for (int k = 0; k < dforx1.size(); k++) {
        if (dforx1[k] != j) ans = min(ans, j - dforx1[k] + 1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
