#include <bits/stdc++.h>
using namespace std;
vector<int> max_div;
void calc_max_div(int n) {
  max_div = vector<int>(n + 1, 0);
  for (int i = 2; i <= n; ++i) {
    if (max_div[i] == 0) {
      for (int j = i; j <= n; j += i) {
        max_div[j] = i;
      }
    }
  }
}
int minprev(int n) {
  return n - max_div[n] + 1 == 1 ? 2147483647 : n - max_div[n] + 1;
}
int solve(int n) {
  int ans = 2147483647;
  for (int i = minprev(n); i <= n; ++i) {
    ans = min(ans, minprev(i));
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  calc_max_div(n);
  cout << solve(n);
  return 0;
}
