#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int N1 = n;
  int P = 0;
  for (int i = 2; i <= sqrt(n); ++i) {
    if (N1 % i == 0) {
      P = max(P, i);
      while (N1 % i == 0) N1 = N1 / i;
    }
  }
  if (N1 > 1) P = max(P, N1);
  P = n - P;
  int ans = n;
  for (int i = P + 1; i < n; ++i) {
    int temp = i;
    int ans2 = 0;
    for (int j = 2; j <= sqrt(temp); ++j) {
      if (temp % j == 0) {
        ans2 = max(ans2, j);
        while (temp % j == 0) temp = temp / j;
      }
    }
    if (temp > 1) ans2 = max(ans2, temp);
    if (temp != i) {
      if (temp > 1) ans2 = max(ans2, temp);
      temp = i - ans2 + 1;
      ans = min(ans, temp);
    } else
      ans = min(ans, i);
  }
  cout << ans;
}
