#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int sv[N];
int max_div[N];
int calc(int x) {
  if (sv[x] == x) return x;
  int a = max_div[x];
  int b = x / max_div[x];
  return (b - 1) * a + 1;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  for (int i = 0; i < N; ++i) sv[i] = i;
  for (int i = 2; i * i < N; ++i) {
    if (sv[i] == i) {
      for (int j = i * i; j < N; j += i) sv[j] = min(sv[j], i);
    }
  }
  for (int i = 2; i < N; ++i) {
    if (sv[i] == i)
      max_div[i] = i;
    else
      max_div[i] = max(max_div[i / sv[i]], sv[i]);
  }
  int x;
  cin >> x;
  if (sv[x] == x) {
    cout << x;
    return 0;
  }
  int l = calc(x);
  int r = x - 1;
  int ans = calc(l);
  for (; l <= r; ++l) {
    ans = min(ans, calc(l));
  }
  cout << ans;
}
