#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> divide(int m) {
  vector<pair<int, int>> res;
  for (int i = 2; i * i <= m; i++) {
    int t = 0;
    if (m % i == 0) {
      while (m % i == 0) {
        m /= i;
        t++;
      }
      res.push_back({i, t});
    }
  }
  if (m != 1) res.push_back({m, 1});
  return res;
}
int trans(int k) {
  int t = divide(k).back().first;
  if (t == k) return t;
  return k - t + 1;
}
int solve(int n) {
  int max_fact = divide(n).back().first;
  int res = -1;
  for (int i = n - max_fact + 1; i < n; i++) {
    int T = trans(i);
    if (res == -1 || T < res) res = T;
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  cout << solve(n) << '\n';
}
