#include <bits/stdc++.h>
using namespace std;
const int __ = 1000000;
bool pri[__ + 5];
int num[__ + 5];
void era() {
  int x = (int)sqrt(__ + 0.01);
  for (int i = 2; i <= x; i++)
    if (!pri[i]) {
      num[++num[0]] = i;
      for (int j = i * i; j <= __; j += i) pri[j] = true;
    }
  for (int i = x + 1; i <= __; i++)
    if (!pri[i]) num[++num[0]] = i;
}
int main() {
  era();
  int n;
  scanf("%d", &n);
  int m = n, maxx = 0;
  for (int i = 2; i * i <= m; i++)
    if (m % i == 0)
      for (maxx = i; m % i == 0; m /= i)
        ;
  if (m != 1) maxx = m;
  int x1 = n - maxx + 1, ans = INT_MAX;
  for (int i = (1); i <= (num[0]); i++) {
    int p = num[i];
    int x0 = (x1 + p - 1) / p * p - (p - 1);
    int x = (x0 + p - 1) / p * p;
    if (x0 > p && x0 >= 3 && x <= n) ans = min(ans, x0);
  }
  printf("%d\n", ans);
  return 0;
}
