#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000011;
bool hsh[MAX];
int fact[MAX];
void sieve(int n) {
  int i, a;
  for (i = 2; i <= n; i++) hsh[i] = true;
  for (i = 2; i <= n; i++)
    if (hsh[i]) {
      a = 2;
      fact[i] = i;
      while (a * i <= n) hsh[a * i] = false, fact[a * i] = i, a++;
    }
}
int factorize(int n) {
  set<int> st;
  int cnt = 0;
  while (n > 1) {
    cnt++;
    st.insert(fact[n]);
    n /= fact[n];
  }
  if (cnt == 1) return -1;
  return *(--st.end());
}
int main() {
  cin.sync_with_stdio(false);
  int n, m;
  sieve(1e6);
  cin >> n;
  m = factorize(n);
  if (m == -1) {
    cout << n;
    return 0;
  }
  int pre = n;
  int temp = n - m + 1;
  int ans = INT_MAX;
  while (n >= temp) {
    m = factorize(n);
    if (m == -1) {
      n--;
      continue;
    }
    ans = min(ans, n - m + 1);
    n--;
  }
  if (ans == INT_MAX) ans = pre;
  cout << ans;
  return 0;
}
