#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MAXN = 1e6 + 69;
const long long MOD = 1e9 + 7;
int n, ans, dp[MAXN];
bool mark[MAXN];
inline void preprocess() {
  for (int i = 2; i <= MAXN; i++) {
    if (!mark[i])
      for (int j = i * 2; j < MAXN; j += i) dp[j] = i, mark[j] = true;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  preprocess();
  ans = INF;
  for (int i = n - dp[n] + 1; i <= n; i++) {
    if (mark[i]) ans = min(ans, i - dp[i] + 1);
  }
  cout << ans;
  return cout << '\n', 0;
}
