#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, L = 20, MOD = 1e9 + 7, M = 10 + 10;
const long long inf = 1e18 + 10;
bool mark[N];
int check(int a) {
  int mx = 0;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) mx = i;
    while (a % i == 0) a /= i;
  }
  if (a > 1) mx = a;
  return mx;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int ans = MOD;
  for (int i = 2; i < N; i++) {
    if (mark[i] == 1) {
      continue;
    }
    for (int j = 2 * i; j < N; j += i) {
      mark[j] = 1;
    }
  }
  int n;
  cin >> n;
  if (mark[n] == 0) {
    cout << n << endl;
    return 0;
  }
  int cn = check(n);
  for (int i = n - cn + 1; i <= n; i++) {
    if (mark[i] == 0 && i >= 3) {
      ans = min(ans, i);
      continue;
    }
    int ci = check(i);
    ans = min(ans, i - ci + 1);
  }
  cout << ans << endl;
  return 0;
}
