#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, mx = 1e6;
long long dp[mx + 10];
int main() {
  long long n;
  cin >> n;
  for (int i = 2; i <= n; i++) {
    if (!dp[i]) {
      for (int j = i * 2; j <= n; j += i) dp[j] = i;
    }
    dp[i] = i - dp[i] + 1;
  }
  long long mx = n;
  for (int i = dp[n]; i <= n; i++) {
    mx = min(mx, dp[i]);
  }
  cout << mx << endl;
}
